I am sure this is an easy question but I found only difficult answers... I just started to do some R programming and I really like the dplyr and ggvis package.. However I could not figure out how to combine multiple line graphs in one diagram. I measured different samples over time and my data looks something like this:
time <-1:10
m = matrix(c(2, 4, 3, 1, 5, 7),nrow=10,ncol=3,byrow = FALSE)
colnames(m)<-c("sample1","sample2","sample3")
mdata <- data.frame(time, m)
data <-tbl_df(data)
I know that it works to chain the layers together but that would not be handy for my data set which contains MANY samples....
data %>% ggvis() %>% layer_paths(~time,~sample1) %>% layer_paths(~time,~sample2)%>%layer_paths(~time, ~sample3)
Any suggestions to make this work with ggvis? I played around with a for loop but without success.. Thank you very much!
答案 0 :(得分:0)
您的问题是,您的数据需要采用长格式而不是宽幅,以便ggvis
能够绘制它。您可以使用tidyr
进行重塑。为了进一步参考,我建议您cheatsheet关于重组数据。
library(ggvis)
library(tidyr)
library(dplyr)
time <- 1:10
m <- matrix(c(2, 4, 3, 1, 5, 7), nrow = 10, ncol = 3, byrow = FALSE)
colnames(m) <- c("sample1", "sample2", "sample3")
mdata <- data.frame(time, m)
data <- tbl_df(mdata)
# gather your data into long format
data <- data %>%
gather(sample, value, -time)
data %>%
ggvis(~time, ~value, stroke = ~sample) %>%
layer_lines()