繁体   English   中英

如何在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完成了此summarySE ,但是,它不再与当前版本的 R 兼容。我尝试使用两个单独的数据框(一个包含单个数据,一个包含平均数据)并覆盖这些数据,但是我想因为我已经融化了单个数据(从 35x102 数据框到 3x3570),我收到一个错误消息:

“美学必须是长度为 1 或与数据 (102) 相同:组”。

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

错误:geom_line 需要以下缺失的美学: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")

这是我需要作为输出的示例数据框和图形的保管箱链接

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

您可以从摘要数据框中添加一个层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()

输出

output_stat_summary

要制作类似于您期望的输出(如您共享的链接所示),

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()

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

输出

output_geom_rec

你可以参考这里se功能。

你有没有尝试过这样的事情? 只是概括。

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")

这给了你:

具有红色总平均值的多线图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM