我有一个实验,随着时间的推移,已经研究了三种不断进化的酵母种群。在离散的时间点,我们测量了它们的增长,这是响应变量。我基本上想要将酵母的生长绘制为时间序列,使用箱图来总结每个点的测量结果,并分别绘制三个种群的每个种群。基本上,看起来像这样的东西(作为一个新手,我不能发布实际的图像,所以x,y,z指的是三个重复的图像):
| xyz
| x z xyz
| y xyz
| xyz y
| x z
|
-----------------------
t0 t1 t2如何使用ggplot2来实现这一点?我有一种感觉,一定有一个简单而优雅的解决方案,但我找不到。
发布于 2011-10-02 23:32:16
尝试以下代码:
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"))。
https://stackoverflow.com/questions/7625715
复制相似问题