【问题标题】:How to plot the grand mean in ggplot如何在ggplot中绘制大均值
【发布时间】:2019-12-29 06:31:22
【问题描述】:

我正在尝试使用 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),我收到一条错误消息:

“美学必须是长度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")

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

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

【问题讨论】:

    标签: r ggplot2 time-series mean line-plot


    【解决方案1】:

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

    输出

    要做出您期望的输出(如您共享的链接所示),

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

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

    输出

    您可以参考here 获取se 功能。

    【讨论】:

    • 谢谢!使用 stat_summary 的第二个输出正是我想要的。我没有考虑添加辅助变量。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      这不像 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")
      

      这给了你:

      【讨论】:

        猜你喜欢
        • 2018-06-09
        • 2019-05-11
        • 2018-06-17
        • 2020-08-04
        • 1970-01-01
        • 2020-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多