我正在尝试为数据集中的每个个人(ID)创建一个时间序列图。
示例数据:
ID <- rep(c(2:5), each = 9, times = 4)
Attitude <- rep(c('A1', 'A2','A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9'), 16)
Answer <- rep(1:5, length.out = 144)
time <- as.character(rep(c(0, 1, 3, 4), each = 9, times = 4))
first_answer <- rep(1:5, length.out = 144)
df <- data.frame(ID, Attitude, Answer, time, first_answer)
df$time <- as.character(df$time)
我当前正在使用的功能代码:
library(dplyr)
spaghetti_plot <- function(input, MV, item_level){
MV <- enquo(MV)
titles <- enquo(item_level)
input %>%
filter(!!(MV) == item_level) %>%
mutate(first_answer = first_answer) %>%
ggplot(.,aes( x = time, y = jitter(Answer), group = ID)) +
geom_line(aes(colour = first_answer)) +
labs(title = titles ,x = 'Time', y = 'Answer', colour = 'Answer given at time 0')
}
这给了我一个图表,其中每个人都有一条线,即所有个人都有一个图(等于的数量ID
)。取而代之的是,我想用#panel =绘制1个图ID
。例如,如果我有10个人的数据,我希望有一个包含10个面板的图形。
我尝试使用facet_wrap
并facet_panel
完成工作,但还没有找到合适的解决方案。
最初是在SAS
。
EDIT2解决方案在注释中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句