如何在ggplot中绘制总均值

How to plot the grand mean in ggplot

我正在尝试使用 ggplotgeom_line 绘制 35 个单独的时间序列数据(每个 102 个数据点)。我还想将各个数据的总均值跨时间重叠为第二个 geom_line,它可以是不同的颜色或不同的 alpha。

这是我的数据示例:

> dput(head(mdata, 10))
structure(list(Individual = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), Signal = c(-0.132894911, -0.13, 0, 0, 0, 0.02, 0.01, 
0.01, 0, 0.02), Time = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 
0.8, 0.9)), row.names = c(NA, 10L), class = "data.frame")

我之前用 summarySE 做过这个,但是,它不再兼容当前版本的 R。我尝试使用两个单独的数据帧(一个包含单独的数据,一个包含平均数据)并覆盖这些数据,但我认为因为我已经融化了单个数据(从 35x102 数据帧到 3x3570),我收到一条错误消息:

"Aesthetics must be either length 1 or the same as the data (102): group".

然后,我尝试使用 stat_summaryfun.data,但我仍然收到错误消息:

Error: geom_line requires the following missing aesthetics: y

ggplot(data=mdata,aes(x=Time, y=Signal, group=Individual, ymin=-1, ymax=3))+ 
  geom_line()+
  stat_summary(fun.data="mean", geom="line", color = "red")

这是我需要作为输出的示例数据框和图表的 dropbox link

如有任何建议,我们将不胜感激!我在其他地方看到过类似的问题,但我认为我在美学范围内对数据进行分组这一事实给我带来了问题。

你试过这样的事情吗?概括一下。

df2<-co2+10

ts1<-ts(co2)
ts2<-ts(df2)
ts3<-ts((ts1+ts2)/2) # In your case the mean can be calculated with a more dedicated function

require(ggplot2)

ggplot()+geom_line(aes(x=1:length(ts1),y=ts1,group=1))+geom_line(aes(x=1:length(ts2),y=ts2,group=2))+
  geom_line(aes(x=1:length(ts3),y=ts3,group=3,color="red"))+labs(color="Grandmean",x="Time",y="Serie")

这不像 stat_summary 那样优雅,但您可以通过以下方式获得总均值:

by_time <- group_by(df, Time)
s <- summarise(by_time, meanSignal = mean(Signal, na.rm=T))
s
# A tibble: 102 x 2
    Time meanSignal
   <dbl>      <dbl>
 1   0    -1.16e- 1
 2   0.1  -1.15e- 1
 3   0.2  -9.14e- 3
 4   0.3   4.57e- 3

然后使用两个数据框 df 和 s 进行绘图。

ggplot(df, aes(x= Time, y = Signal))+geom_line(alpha = 0.25,aes(group=Individual))+geom_line(data=s, aes(x = Time, y = meanSignal), color="#FF0000")

这给你:

您可以从汇总数据框中添加层 geom_line()

# Let's create the summary using `dplyr'
library(dplyr)
avg_group <- mdata %>% 
  select(Individual, Signal, Time) %>%
  group_by(Individual) %>% 
  summarise(avg_ind = mean(Time), avg_sig = mean(Signal))
# -------------------------------------------------------------------------
# > avg_group
# # A tibble: 35 x 3
# Individual avg_ind avg_sig
# <int>   <dbl>   <dbl>
# 1          1    5.05  0.107 
# 2          2    5.05  0.0947
# 3          3    5.05  0.0781
# 4          4    5.05  0.0362
# 5          5    5.05  0.0156
# 6          6    5.05  0.0182
# 7          7    5.05  0.774 
# 8          8    5.05  0.297 
# 9          9    5.05  0.517 
# 10         10    5.05  0.685 
# # … with 25 more rows
# -------------------------------------------------------------------------
# Then plot the graph using 
ggplot(mdata,aes(x=Time, y=Signal, group=Individual, ymin=-1, ymax=3))+ 
  geom_line() + 
  geom_line(data = avg_group, aes(avg_ind, avg_sig), group = 1, color = "red") + theme_bw()
# -------------------------------------------------------------------------

输出

如果您更喜欢stat_summary(),您可以做的是添加一个数据框共有的显式变量并将其用作分组aesthetic。您可以按如下方式进行:

# > head(mdata, 2)
# Individual     Signal Time
# 1          1 -0.1328949  0.0
# 2          1 -0.1300000  0.1
# ------------------------------------------------------------------------
mdata$grand <- 1 

# > head(mdata, 2)
# Individual     Signal Time grand
# 1          1 -0.1328949  0.0     1
# 2          1 -0.1300000  0.1     1
# ------------------------------------------------------------------------
# plot using grand as an explicit variable used to group the plot
ggplot(mdata,aes(x=Time, y=Signal, group=Individual, ymin=-1, ymax=3))+ 
  geom_line() + stat_summary(aes(group = grand), fun.y="mean", geom="line", color = "red") + theme_bw()

输出

要制作出您期望的输出(如您分享的 link 所示),

ggplot(data=mdata,aes(x=Time, y=Signal, group=Individual, ymin=-1, ymax=3))+ 
  geom_line()+ 
  geom_rect(xmin = (mean(mdata$Time) + se(mdata$Time)) , xmax =xmin + 0.4, fill = "red", ymax = -0.94, ymin = -1) + theme_bw()

此输出有一个警告,因为并非所有数据都来自数据,尽管总均值和标准误差用于绘制矩形。

输出

se函数可以参考here