我正在尝试创建一个基于ggplot2的简单函数来节省空间。
示例数据:
X <- rep(c(9:15), each = 9)
ID <- rep(c(2:8), each = 9)
Attitude <- rep(c('A1', 'A2','A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9'), 7)
Answer_a <- rep(1:5, length.out = 63)
time <- as.character(rep(c(0, 1, 3), each = 21))
first_answer <- rep(1:5, length.out = 63)
df <- data.frame(X, ID, Attitude, Answer_a, time, first_answer)
df$time <- as.character(df$time)
我想要创建的函数的代码:
spaghetti_plot <- function(input, MV, item_level){
p <- ggplot(na.omit(input[ which(input$MV == item_level), ]),
aes( x = time, y = Answer_a, group = ID))
p <- p + geom_line(aes(colour = first_answer))
return(p)
}
所以从本质上讲,我的问题是为什么我在使用函数时收到警告消息Warning: Unknown or uninitialised column
,如下所示:
spaghett_plot(df,Attitude,'A1')