首页 > 解决方案 > 为每天的平均值添加线(ggplot)

问题描述

我有一个关于网页视图的数据集,下面是一个示例。该数据集一直持续到 8 月。UPV 是唯一的页面浏览量。

Datum      Page                  UPV 
  <date>     <fct>               <int>  
1 2020-03-16 Page1                  548  
2 2020-03-16 Page2                  355  
3 2020-03-16 Page3                  140    
4 2020-03-17 Page2                  883  
5 2020-03-17 Page1                  337  
6 2020-03-17 Page3                  76 

我已经能够为单个因子水平创建分面图:

ggplot(webviews, aes(Datum, UPV, col=Page)) + geom_col(width = 0.3, position = position_dodge(width = 0.9)) + facet_wrap(Page ~ .) + theme_bw()+ 
  theme(plot.background = element_rect(fill = "black"), panel.background = element_rect(fill = 'black'),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  # Facet grid
  theme(
    strip.background = element_rect(
      color="#535353", fill="black", size=.004, linetype="solid"))+ theme( 
        strip.text.x = element_text(size = 10, color = "grey", face="bold")) + theme(legend.position = "none") + 
  labs(
    title = "\n Title \n", caption = "Caption \n") + 
  theme (
    plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) + theme (
    plot.title = element_text(color = "grey", size = 12, face = "bold"), 
    plot.subtitle = element_text(color = "grey"), 
    plot.caption = element_text(color = "#696969", size = 7, face = "italic", hjust=0.5))

到目前为止,这没问题。仍在玩情节结果本身。

我想在每个图中添加一个元素:每天(所有页面的)平均 UPV 的一行,因此您可以轻松查看页面得分是高于还是低于当天的平均值。我已经尝试过了,但我无法将这个 df 连接到原始图:

webviews_mean <- webviews %>% group_by(Datum) %>%
  summarize(mean = mean(UPV))

我希望我在这里想要做的事情有点清楚......我对此仍然很陌生,并且因为尝试一些事情而有点脑筋急转弯。提前致谢!

标签: r

解决方案


我建议下一个方法:

library(tidyverse)
#Data
webviews <- structure(list(Datum = c("16/03/2020", "16/03/2020", "16/03/2020", 
"17/03/2020", "17/03/2020", "17/03/2020"), Page = c("Page1", 
"Page2", "Page3", "Page2", "Page1", "Page3"), UPV = c(548L, 355L, 
140L, 883L, 337L, 76L)), class = "data.frame", row.names = c(NA, 
-6L))

代码:

#Aggregate for means
webviews_mean <- webviews %>% group_by(Page) %>%
  summarize(Mean = mean(UPV)) %>% ungroup()
#Plots
ggplot(webviews, aes(Datum, UPV, col=Page)) + 
  geom_col(width = 0.3, position = position_dodge(width = 0.9)) + 
  geom_hline(data = webviews_mean,aes(yintercept = Mean),color='white',lty='dashed')+
  facet_wrap(Page ~ .) + theme_bw()+ 
  theme(plot.background = element_rect(fill = "black"), panel.background = element_rect(fill = 'black'),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  # Facet grid
  theme(
    strip.background = element_rect(
      color="#535353", fill="black", size=.004, linetype="solid"))+ theme( 
        strip.text.x = element_text(size = 10, color = "grey", face="bold")) + theme(legend.position = "none") + 
  labs(
    title = "\n Title \n", caption = "Caption \n") + 
  theme (
    plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) + theme (
      plot.title = element_text(color = "grey", size = 12, face = "bold"), 
      plot.subtitle = element_text(color = "grey"), 
      plot.caption = element_text(color = "#696969", size = 7, face = "italic", hjust=0.5))

输出:

在此处输入图像描述

虚线显示了每个方面的平均值。当然你可以修改它。


推荐阅读