r - 为每天的平均值添加线(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))
我希望我在这里想要做的事情有点清楚......我对此仍然很陌生,并且因为尝试一些事情而有点脑筋急转弯。提前致谢!
解决方案
我建议下一个方法:
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))
输出:
虚线显示了每个方面的平均值。当然你可以修改它。
推荐阅读
- nextflow - 在 Nextflow 中获取两个输出文件时出现问题
- python - 如何使用 for 循环/范围来结束 Python 中的特定字符输入?
- blockchain - 智能合约没有产生结果,我如何查看两个结果或获得两个结果的总数
- javascript - TypeScript 找不到名称“SpeechGrammarList”,在“DOM”中实现
- c# - 我正在尝试用 C# 读取、排序然后写入
- bash - 如何使用netcat接收txt文件的内容
- python - 即使在类属性中满足要求时也会引发 AssertionError?
- multithreading - Rust 中的多线程下载时出现奇怪的错误
- regex - 如何根据键正则表达式查找值
- python - Python Selenium 查找所有 CSS 元素并替换所有