使用ggplot2的时间序列图 [英] Time series plot with groups using ggplot2

查看:229
本文介绍了使用ggplot2的时间序列图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实验,随着时间的推移研究了三个不断发展的酵母种群。在离散的时间点,我们测量了他们的增长,这是响应变量。我基本上想要将酵母的增长作为一个时间序列来绘制,使用箱形图来总结每个点的测量结果,并分别绘制三个种群的每一个。基本上,看起来像这样的东西(作为一个新手,我不会发布实际的图像,所以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)+ rorm(30) b $ b group = rep(c(x,y,z),10,each = 3)

df $ time < - factor(格式(df $ time ,格式=%Y-%m-%d))

p < - ggplot(df,aes(x = time,y = y,fill = group))+ geom_boxplot b $ b print(p)



只有 x =因子(时间) ggplot(df,aes(x = factor(time),y = y,fill = group))+ geom_boxplot()+ scale_x_date() p>

预处理,因子(格式(df $ time,format =%Y-%m-%d)),是这种形式的图形所必需的。


I have an experiment where three evolving populations of yeast have been studied over time. At discrete time points, we measured their growth, which is the response variable. I basically want to plot the growth of yeast as a time series, using boxplots to summarise the measurements taken at each point, and plotting each of the three populations separately. Basically, something that looks like this (as a newbie, I don't get to post actual images, so x,y,z refer to the three replicates):

|              xyz
|       x z    xyz
|  y    xyz
| xyz    y
| x z     
|
-----------------------
  t0     t1    t2

How can this be done using ggplot2? I have a feeling that there must be a simple and elegant solution, but I can't find it.

解决方案

Try this code:

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)

Only with x = factor(time), ggplot(df, aes(x = factor(time), y = y, fill = group)) + geom_boxplot() + scale_x_date(), was not working.

Pre-processing, factor(format(df$time, format = "%Y-%m-%d")), was required for this form of graphics.

这篇关于使用ggplot2的时间序列图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆