我通过从主数据帧中提取一个小数据帧来绘制与单个用户(表示为 s)相关的答案的时间序列,然后使用下面的代码使用 ggplot 进行绘制:
\ndf_s <- df %>% \n filter(UserId == s) %>%\n select(all_of(c("Answer_Date", questions))) %>%\n melt(id.vars = "Answer_Date", variable.name = "Series")\n\nplt <- df_s %>% \n ggplot(aes(Answer_Date, value)) + \n geom_line(aes(color = Series, linetype = Series)) +\n labs(title = paste0(prefix, s),\n x = "Answer_Date", y = "Response")\n\nshow(plt)\nRun Code Online (Sandbox Code Playgroud)\n我总共绘制了 6 条线,每条线都有不同的颜色和不同的线型,ggplot 很好地支持了这一点。
\n如果可能的话,我也想改变线条的粗细,第一条线较粗,后续的线较细。如果线条的粗细从第一行到最后一行稳步下降,我几乎会同样高兴。我试过
\ngeom_line(aes(color = Series, linetype = Series, size = Series))\nRun Code Online (Sandbox Code Playgroud)\n它可以工作,但是线条太粗了,此外,我还收到以下神秘警告:
\nWarning message:\nUsing size for a discrete variable is not advised. \nRun Code Online (Sandbox Code Playgroud)\n接下来我尝试了这个的变体,例如
\ngeom_line(aes(color = Series, linetype = Series, size = (Series/3)))\nRun Code Online (Sandbox Code Playgroud)\n几乎没有得到一个图表(只有两三个点)以及一个更全面的警告:
\nWarning messages:\n1: In Ops.factor(Series, 3) : \xe2\x80\x98/\xe2\x80\x99 not meaningful for factors\n2: Using size for a discrete variable is not advised. \n3: In Ops.factor(Series, 3) : \xe2\x80\x98/\xe2\x80\x99 not meaningful for factors\n4: Removed 636 row(s) containing missing values (geom_path).\nRun Code Online (Sandbox Code Playgroud)\n我的第三次尝试是
\ngeom_line(aes(颜色 = 系列,线型 = 系列,尺寸 = (1, 0.5.0.5, 0.5, 0.5, 0.5, 0.5)))
\n这导致了不同的错误消息:
\nError: unexpected ',' in:\n" ggplot(aes(Answer_Date, value)) + \n geom_line(aes(color = Series, linetype = Series, size = (1,"\nRun Code Online (Sandbox Code Playgroud)\n如果有人能指出我的问题的解决方案,我将不胜感激。
\n提前谢谢了
\n托马斯·飞利浦
\n也许尝试scale_size_manual()这样使用:
library(ggplot2)
#Data
df <- data.frame(g=rep(c('G1','G2','G3'),each=10),
index=rep(1:10,3),
val=c(cumsum(rnorm(10,0,1)),cumsum(rnorm(10,5,1)),cumsum(rnorm(10,5,5))))
#Plot
ggplot(df,aes(x=index,y=val,group=g))+
geom_line(aes(color=g,size=g))+
scale_size_manual(values=c(0.1,0.75,1.5))
Run Code Online (Sandbox Code Playgroud)
输出: