【问题标题】:ggplot2: add line for average per group (Error: No stat called StatHline.)ggplot2:为每组的平均值添加行(错误:没有称为 StatHline 的统计数据。)
【发布时间】:2016-04-21 07:52:36
【问题描述】:

我最近更新了ggplot2 包并遇到了使用facets 为每组平均值绘制水平线的主要问题。 我相信this 的帖子不再有效?

我正在使用以下代码创建时间序列图:

ggplot(p2p_dt_SKILL_A,aes(x=Date,y=Prod_DL)) +
    geom_line(aes(colour="red"),lwd=1.3) +
    geom_smooth() +
    geom_line(stat = "hline", yintercept = "mean")+
    scale_x_date(labels=date_format("%b-%y"),breaks ="2 month")+
    geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-09-18"]))+

     geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-02"]))+
     geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-23"]))+
    ylab("DL Prod for All Skills")+
    ggtitle("BVG1 DL Prod for All Skills 2014-2015")+
    theme(axis.title.y = element_text(size = 15,face="bold",color="red"),
          plot.title = element_text(size = 15,lineheight = .8,face="bold",color="red"),
          axis.title.x = element_blank(),
          legend.position="none")+
    facet_wrap(~Patch)

第一个问题是我不能再在geom_line(stat = "hline", yintercept = "mean") 中使用stat = "hline",因为它给出了以下错误:Error: No stat called StatHline。 因此,我将其更改为:

ggplot(p2p_dt_SKILL_A,aes(x=Date,y=Prod_DL)) +
    geom_line(aes(colour="red"),lwd=1.3) +
    geom_smooth() +
    geom_hline(yintercept = mean(p2p_dt_SKILL_A$Prod_DL))+
    scale_x_date(labels=date_format("%b-%y"),date_breaks ="2 month")+
    geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-09-18"]))+

     geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-02"]))+
     geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-23"]))+
    ylab("DL Prod for All Skills")+
    ggtitle("BVG1 DL Prod for All Skills 2014-2015")+
    theme(axis.title.y = element_text(size = 15,face="bold",color="red"),
          plot.title = element_text(size = 15,lineheight = .8,face="bold",color="red"),
          axis.title.x = element_blank(),
          legend.position="none")+
    facet_wrap(~Patch)

但这并没有在每个补丁的平均值处绘制水平线。它只取Prod_DL 的整体平均值 见下文:

现在有什么新方法可以计算每组的平均值并绘制水平线吗?

谢谢

更新

这是我所做的:

#first create a dataframe which holds patch and mean values for prod dl, this will then be used in geom_hline()
mean_Prod_DL <- p2p_dt_SKILL_A%>%
                                group_by(Patch)%>%
                                summarise(mean_Prod_DL_per_patch = mean(Prod_DL))


ggplot(p2p_dt_SKILL_A,aes(x=Date,y=Prod_DL)) +
        scale_x_date(labels=date_format("%b-%y"),date_breaks ="2 months")+
        geom_line(aes(colour="red"),lwd=1.3) +
        geom_smooth() +
        geom_hline(data = mean_Prod_DL,aes(yintercept = mean_Prod_DL_per_patch),lty=2)+
        geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-09-18"]))+
         geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-02"]))+
         geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-23"]))+
        geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-12-04"]))+
        ylab("DL Prod for All Skills")+
        ggtitle("BVG1 DL Prod for All Skills 2014-2016")+
        theme(axis.title.y = element_text(size = 15,face="bold",color="red"),
              plot.title = element_text(size = 15,lineheight = .8,face="bold",color="red"),
              axis.title.x = element_blank(),
              legend.position="none")+
        facet_wrap(~Patch)

【问题讨论】:

  • 您可以为每个方面创建一个带有 yintercept 的新数据集
  • @MLavoie 有没有更好的解决方案来绘制每组均值的水平线?
  • 也许有人会想出更好的解决方案。
  • 我完全按照你说的做了,但之前的版本做得很顺利。使用了文档中的示例# To show different lines in different facets, use aesthetics p &lt;- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(~ cyl) mean_wt &lt;- data.frame(cyl = c(4, 6, 8), wt = c(2.28, 3.11, 4.00)) p + geom_hline(aes(yintercept = wt), mean_wt)
  • here 的另一种巧妙方法,不需要在 ggplot 之外进行任何计算。所以只需添加另一个geom_smooth 行.. geom_smooth(se=FALSE, method="lm", formula=y~1, colour="black")

标签: r ggplot2 group-by mean stat


【解决方案1】:

我同意@MLavoie 的观点,即仅计算感兴趣的数量是最简单的解决方案。不确定您以何种方式寻找“更好”的东西。

例子:

# sample data
my_df <- data.frame(x=rep(1:100, 4),
                    y=cumsum(rnorm(400)),
                    category=rep(letters[1:4], each=100))

# calculate the hline data in one line with data.table
library(data.table)
setDT(my_df)[, cat_mean := mean(y), by=category]

# plot
ggplot(my_df, aes(x=x, y=y, group=category)) +
  geom_line(color='red') +
  geom_smooth(color='blue') +
  geom_hline(aes(yintercept=cat_mean)) +
  facet_wrap(~category)

结果:

【讨论】:

  • 与您所做的略有不同...但仍然是同一件事...添加了答案
猜你喜欢
  • 2016-04-17
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 2013-03-17
相关资源
最近更新 更多