作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为我拥有的每一年的时间序列 ggplot 图添加“3”月的“平均”线。
我想通过 facets
的平均值画一条水平线- 我试过group_by
, mutate
等,但不能让它工作。
预期输出将只是基于每个方面的水平线,基于该方面的月“3”平均值。
代码:
dat %>%
ggplot(aes(x = day, y = NO2)) +
geom_line() +
facet_grid(~year)
dat <- structure(list(station_location = c("Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen", "Pza del Carmen", "Pza del Carmen", "Pza del Carmen",
"Pza del Carmen"), date = structure(c(16495, 16496, 16497, 16498,
16499, 16500, 16501, 16502, 16503, 16504, 16505, 16506, 16507,
16508, 16509, 16510, 16511, 16512, 16513, 16514, 16515, 16516,
16517, 16518, 16519, 16520, 16521, 16522, 16523, 16524, 16525,
16861, 16862, 16863, 16864, 16865, 16866, 16867, 16868, 16869,
16870, 16871, 16872, 16873, 16874, 16875, 16876, 16877, 16878,
16879, 16880, 16881, 16882, 16883, 16884, 16885, 16886, 16887,
16888, 16889, 16890, 16891, 17226, 17227, 17228, 17229, 17230,
17231, 17232, 17233, 17234, 17235, 17236, 17237, 17238, 17239,
17240, 17241, 17242, 17243, 17244, 17245, 17246, 17247, 17248,
17249, 17250, 17251, 17252, 17253, 17254, 17255, 17256, 17591,
17592, 17593, 17594, 17595, 17596, 17597, 17598, 17599, 17600,
17601, 17602, 17603, 17604, 17605, 17606, 17607, 17608, 17609,
17610, 17611, 17612, 17613, 17614, 17615, 17616, 17617, 17618,
17619, 17620, 17621, 17956, 17957, 17958, 17959, 17960, 17961,
17962, 17963, 17964, 17965, 17966, 17967, 17968, 17969, 17970,
17971, 17972, 17973, 17974, 17975, 17976, 17977, 17978, 17979,
17980, 17981, 17982, 17983, 17984, 17985, 17986), class = "Date"),
yvar = c(31, 35, 51, 55, 50, 62, 83, 62, 74, 80, 82, 77,
54, 38, 39, 48, 54, 49, 36, 36, 36, 37, 58, 41, 32, 38, 44,
57, 40, 54, 69, 70, 57, 48, 45, 35, 33, 39, 46, 46, 43, 50,
50, 49, 51, 54, 72, 64, 47, 35, 41, 53, 46, 55, 44, 48, 35,
21, 23, 34, 53, 34, 42, 53, 48, 26, 25, 34, 45, 71, 79, 80,
59, 31, 30, 43, 44, 45, 58, 65, 59, 43, 33, 29, 37, 50, 48,
30, 32, 47, 59, 57, 44, 39, 60, 33, 34, 37, 39, 41, 57, 55,
42, 21, 28, 48, 47, 28, 35, 33, 38, 44, 44, 51, 59, 60, 31,
39, 55, 47, 47, 28, 20, 30, 56, 49, 34, 13, 22, 16, 18, 34,
38, 38, 41, 37, 29, 39, 58, NA, NA, NA, 29, 30, 42, 44, 33,
29, 32, 24, 44, 48, 34, 27, 26), year = c(2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L),
month = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), day = c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-155L))
最佳答案
您可以使用 geom_smooth,并适合仅拦截线:
dat %>%
ggplot(aes(x = day, y = yvar)) +
geom_line() +
geom_smooth(method="lm",formula=y~1,se=FALSE)+
facet_grid(~year)
关于r - ggplot 将平均线添加到特定月份的方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60908172/
我是一名优秀的程序员,十分优秀!