我有每周收集的数据。我想要一个显示一系列小提琴图的图表,每周一个。最重要的是,我想要一条趋势线。问题似乎在于,为了分离小提琴图,Week 变量必须是一个因素,但为了绘制趋势线,Week 变量必须是连续的。我如何获得两者?
Week = as.Date(c("2017-10-1", "2017-10-8", "2017-10-15"))
mydata = data.frame(Week = sample(Week, 200, T), v_1 = sample.int(5, 200, T))
p1 = ggplot(mydata, aes(x = factor(Week), y = v_1)) # create the plot stub
p1 + geom_violin() # just violin plots
p1 + geom_smooth(method = "lm") # nothing
p1 + geom_violin() + geom_smooth() # just violin plots
p2 = ggplot(mydata, aes(x = Week, y = v_1)) # new plot stuf
p2 + geom_violin() # single violin plot, not separated by week
p2 + geom_smooth(method = "lm") # trend line
p2 + geom_violin() + geom_smooth(method = "lm") # trend line over single violin plot