TransWikia.com

multiple line plot in R from multiple timeseries

Stack Overflow Asked on December 3, 2021

I have a series of timings.

I’m trying to plot them as individual lines, I use the %>% group_by(runs), but I get the error: geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? where have I gone wrong?

All the necessary code and data is included in the below code snippet.

library(lubridate)
library(dplyr)
runs <- c("10000", "20000","50000","75000","100000","200000","300000","400000","500000","600000","700000","800000","900000","1000000")
k10 <- c("0:00:01","0:00:03","0:00:11","0:00:16","0:00:22","0:00:48","0:01:12","0:01:38","0:02:04","0:02:29","0:02:59","0:03:23","0:03:49","0:04:15")
k20 <- c("0:00:03","0:00:06","0:00:23","0:00:33","0:00:44","0:01:37","0:02:27","0:03:18","0:04:10","0:05:03","0:05:57","0:06:45","0:07:38","0:08:33")
k50 <- c("0:00:07","0:00:14","0:00:56","0:01:23","0:01:49","0:04:04","0:06:08","0:08:19","0:10:27","0:12:37","0:14:55","0:16:53","0:19:04","0:21:20")
k75 <- c("0:00:11","0:00:21","0:01:24","0:02:05","0:02:44","0:06:05","0:09:12","0:12:22","0:15:38","0:18:56","0:22:22","0:25:21","0:28:40","0:32:04")
k100 <- c("0:00:15","0:00:28","0:01:53","0:02:45","0:03:39","0:08:07","0:12:17","0:16:29","0:20:43","0:24:56","0:29:27","0:33:20","0:37:52","0:42:32")
k200 <- c("0:00:29","0:00:56","0:03:47","0:05:31","0:07:13","0:16:08","0:24:35","0:33:02","0:41:47","0:51:42","1:01:11","1:08:21","1:17:44","1:26:20")
k300 <- c("0:00:45","0:01:26","0:05:46","0:08:26","0:11:06","0:25:02","0:37:42","0:50:29","1:03:25","1:16:12","1:30:22","1:42:37","1:56:04","2:09:45")
k400 <- c("0:00:59","0:01:53","0:07:36","0:11:12","0:14:46","0:33:13","0:50:01","1:07:44","1:25:21","1:42:27","2:02:14","2:16:48","2:34:49","2:51:18")
k500 <- c("0:01:16","0:02:26","0:09:40","0:14:11","0:18:43","0:42:34","1:03:51","1:24:41","1:46:19","2:08:28","2:30:19","2:50:32","3:13:16","3:35:59")
k600 <- c("0:01:31","0:02:54","0:11:36","0:17:02","0:22:27","0:50:57","1:16:08","1:41:52","2:08:31","2:34:50","3:01:37","3:25:43","4:02:04","4:23:58")
k700 <- c("0:01:50","0:03:30","0:13:35","0:20:00","0:26:23","1:00:05","1:31:41","2:01:26","2:30:03","3:01:47","3:35:24","4:12:10","4:45:46","5:11:17")
k800 <- c("0:02:03","0:04:01","0:15:40","0:22:57","0:30:09","1:08:12","1:41:59","2:17:02","2:52:59","3:33:07","4:10:31","4:39:18","5:17:31","5:50:03")
k900 <- c("0:02:19","0:04:31","0:17:37","0:25:44","0:34:00","1:17:01","1:55:09","2:34:44","3:20:17","4:09:13","4:57:57","5:38:23","6:22:07","6:51:39")
m1 <- c("0:02:34","0:05:02","0:19:48","0:29:22","0:38:30","1:27:17","2:10:32","2:54:40","3:40:49","4:28:53","5:17:31","5:59:44","6:46:24","7:35:39")

k10 <- as.numeric(hms(k10))/60
k20 <- as.numeric(hms(k20))/60
k50 <- as.numeric(hms(k50))/60
k75 <- as.numeric(hms(k75))/60
k100 <- as.numeric(hms(k100))/60
k200 <- as.numeric(hms(k200))/60
k300 <- as.numeric(hms(k300))/60
k400 <- as.numeric(hms(k400))/60
k500 <- as.numeric(hms(k500))/60
k600 <- as.numeric(hms(k600))/60
k700 <- as.numeric(hms(k700))/60
k800 <- as.numeric(hms(k800))/60
k900 <- as.numeric(hms(k900))/60
m1 <- as.numeric(hms(m1))/60

k10 <- as.data.frame(cbind("10000", k10))
k20 <- as.data.frame(cbind("20000", k20))
k50 <- as.data.frame(cbind("50000", k50))
k75 <- as.data.frame(cbind("75000", k75))
k100 <- as.data.frame(cbind("100000", k100))
k200 <- as.data.frame(cbind("200000", k200))
k300 <- as.data.frame(cbind("300000", k300))
k400 <- as.data.frame(cbind("400000", k400))
k500 <- as.data.frame(cbind("500000", k500))
k600 <- as.data.frame(cbind("600000", k600))
k700 <- as.data.frame(cbind("700000", k700))
k800 <- as.data.frame(cbind("800000", k800))
k900 <- as.data.frame(cbind("900000", k900))
m1 <- as.data.frame(cbind("1000000", m1))

colnames(k10)[2] <- "times"
colnames(k20)[2] <- "times"
colnames(k50)[2] <- "times"
colnames(k75)[2] <- "times"
colnames(k100)[2] <- "times"
colnames(k200)[2] <- "times"
colnames(k300)[2] <- "times"
colnames(k400)[2] <- "times"
colnames(k500)[2] <- "times"
colnames(k600)[2] <- "times"
colnames(k700)[2] <- "times"
colnames(k800)[2] <- "times"
colnames(k900)[2] <- "times"
colnames(m1)[2] <- "times"

df <- rbind(k10,k20,k50,k75,k100,k200,k300,k400,k500,k600,k700,k800,k900,m1)
colnames(df)[1] <- "runs"
p2 <- df %>% group_by(runs)

ggplot(p2, aes(runs, times, colour = runs)) +
  geom_line()

2 Answers

library(lubridate)
library(dplyr)
library(ggforce)
runs <- c("10000", "20000","50000","75000","100000","200000","300000","400000","500000","600000","700000","800000","900000","1000000")
k10 <- c("0:00:01","0:00:03","0:00:11","0:00:16","0:00:22","0:00:48","0:01:12","0:01:38","0:02:04","0:02:29","0:02:59","0:03:23","0:03:49","0:04:15")
k20 <- c("0:00:03","0:00:06","0:00:23","0:00:33","0:00:44","0:01:37","0:02:27","0:03:18","0:04:10","0:05:03","0:05:57","0:06:45","0:07:38","0:08:33")
k50 <- c("0:00:07","0:00:14","0:00:56","0:01:23","0:01:49","0:04:04","0:06:08","0:08:19","0:10:27","0:12:37","0:14:55","0:16:53","0:19:04","0:21:20")
k75 <- c("0:00:11","0:00:21","0:01:24","0:02:05","0:02:44","0:06:05","0:09:12","0:12:22","0:15:38","0:18:56","0:22:22","0:25:21","0:28:40","0:32:04")
k100 <- c("0:00:15","0:00:28","0:01:53","0:02:45","0:03:39","0:08:07","0:12:17","0:16:29","0:20:43","0:24:56","0:29:27","0:33:20","0:37:52","0:42:32")
k200 <- c("0:00:29","0:00:56","0:03:47","0:05:31","0:07:13","0:16:08","0:24:35","0:33:02","0:41:47","0:51:42","1:01:11","1:08:21","1:17:44","1:26:20")
k300 <- c("0:00:45","0:01:26","0:05:46","0:08:26","0:11:06","0:25:02","0:37:42","0:50:29","1:03:25","1:16:12","1:30:22","1:42:37","1:56:04","2:09:45")
k400 <- c("0:00:59","0:01:53","0:07:36","0:11:12","0:14:46","0:33:13","0:50:01","1:07:44","1:25:21","1:42:27","2:02:14","2:16:48","2:34:49","2:51:18")
k500 <- c("0:01:16","0:02:26","0:09:40","0:14:11","0:18:43","0:42:34","1:03:51","1:24:41","1:46:19","2:08:28","2:30:19","2:50:32","3:13:16","3:35:59")
k600 <- c("0:01:31","0:02:54","0:11:36","0:17:02","0:22:27","0:50:57","1:16:08","1:41:52","2:08:31","2:34:50","3:01:37","3:25:43","4:02:04","4:23:58")
k700 <- c("0:01:50","0:03:30","0:13:35","0:20:00","0:26:23","1:00:05","1:31:41","2:01:26","2:30:03","3:01:47","3:35:24","4:12:10","4:45:46","5:11:17")
k800 <- c("0:02:03","0:04:01","0:15:40","0:22:57","0:30:09","1:08:12","1:41:59","2:17:02","2:52:59","3:33:07","4:10:31","4:39:18","5:17:31","5:50:03")
k900 <- c("0:02:19","0:04:31","0:17:37","0:25:44","0:34:00","1:17:01","1:55:09","2:34:44","3:20:17","4:09:13","4:57:57","5:38:23","6:22:07","6:51:39")
m1 <- c("0:02:34","0:05:02","0:19:48","0:29:22","0:38:30","1:27:17","2:10:32","2:54:40","3:40:49","4:28:53","5:17:31","5:59:44","6:46:24","7:35:39")

k10 <- as.numeric(hms(k10))/60
k20 <- as.numeric(hms(k20))/60
k50 <- as.numeric(hms(k50))/60
k75 <- as.numeric(hms(k75))/60
k100 <- as.numeric(hms(k100))/60
k200 <- as.numeric(hms(k200))/60
k300 <- as.numeric(hms(k300))/60
k400 <- as.numeric(hms(k400))/60
k500 <- as.numeric(hms(k500))/60
k600 <- as.numeric(hms(k600))/60
k700 <- as.numeric(hms(k700))/60
k800 <- as.numeric(hms(k800))/60
k900 <- as.numeric(hms(k900))/60
m1 <- as.numeric(hms(m1))/60

k10 <- as.data.frame(cbind(runs, 10000, k10))
k20 <- as.data.frame(cbind(runs, 20000, k20))
k50 <- as.data.frame(cbind(runs, 50000, k50))
k75 <- as.data.frame(cbind(runs, 75000, k75))
k100 <- as.data.frame(cbind(runs,100000, k100))
k200 <- as.data.frame(cbind(runs, 200000, k200))
k300 <- as.data.frame(cbind(runs, 300000, k300))
k400 <- as.data.frame(cbind(runs, 400000, k400))
k500 <- as.data.frame(cbind(runs, 500000, k500))
k600 <- as.data.frame(cbind(runs, 600000, k600))
k700 <- as.data.frame(cbind(runs, 700000, k700))
k800 <- as.data.frame(cbind(runs, 800000, k800))
k900 <- as.data.frame(cbind(runs, 900000, k900))
m1 <- as.data.frame(cbind(runs, 1000000, m1))

colnames(k10)[3] <- "times"
colnames(k20)[3] <- "times"
colnames(k50)[3] <- "times"
colnames(k75)[3] <- "times"
colnames(k100)[3] <- "times"
colnames(k200)[3] <- "times"
colnames(k300)[3] <- "times"
colnames(k400)[3] <- "times"
colnames(k500)[3] <- "times"
colnames(k600)[3] <- "times"
colnames(k700)[3] <- "times"
colnames(k800)[3] <- "times"
colnames(k900)[3] <- "times"
colnames(m1)[3] <- "times"

df <- rbind(k10,k20,k50,k75,k100,k200,k300,k400,k500,k600,k700,k800,k900,m1)
colnames(df)[2] <- "run_group"

q1 <- df %>% group_by(run_group)

q1$runs <- as.numeric(as.character(q1$runs))
q1$run_group <- as.character(q1$run_group)
q1$times <- as.numeric(as.character(q1$times))

ggplot(q1, aes(runs, times, colour = run_group)) +
  geom_point() +
  geom_line()

Answered by Spatial Digger on December 3, 2021

You can try this changes over df (stage is only an index for axis reference of data and keep original names for df).

library(tidyverse)
#Create index
df %>% group_by(V1) %>% mutate(stage=1:n()) -> df1
df1 %>% ungroup() %>% mutate(times=as.numeric(as.character(times))) -> df1

#Plot
ggplot(df1, aes(x=stage,y=times, colour = V1)) +
  geom_line()

enter image description here

Update

Try this:

ggplot(p2, aes(x=as.numeric(as.character(runs)), y=as.numeric(as.character(times)), colour = runs,group=runs)) +

geom_line()

enter image description here

Answered by Duck on December 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP