我有一个实验,其中随着时间的推移研究了三个进化中的酵母种群。在离散的时间点,我们测量了它们的增长,这是响应变量。我基本上是想按时间序列绘制酵母的生长图,使用箱线图总结每个点的测量值,然后分别绘制三个种群的图。基本上,看起来像这样(作为一个新手,我不发布实际图像,因此x,y,z指的是三个副本):
| xyz
| x z xyz
| y xyz
| xyz y
| x z
|
-----------------------
t0 t1 t2
如何使用ggplot2完成此操作?我觉得必须有一个简单而优雅的解决方案,但是我找不到。
最佳答案
试试这个代码:
require(ggplot2)
df <- data.frame(
time = rep(seq(Sys.Date(), len = 3, by = "1 day"), 10),
y = rep(1:3, 10, each = 3) + rnorm(30),
group = rep(c("x", "y", "z"), 10, each = 3)
)
df$time <- factor(format(df$time, format = "%Y-%m-%d"))
p <- ggplot(df, aes(x = time, y = y, fill = group)) + geom_boxplot()
print(p)
仅使用
x = factor(time)
,ggplot(df, aes(x = factor(time), y = y, fill = group)) + geom_boxplot() + scale_x_date()
无效。这种形式的图形需要进行预处理,
factor(format(df$time, format = "%Y-%m-%d"))
。