使用mtcars
作为示例数据集,可以这样实现:
在您的情况下,您必须加入time
并可能重命名您的变量。
library(dplyr)
library(ggplot2)
mtcars1 <- mtcars %>%
group_by(cyl) %>%
summarise(y_med = median(mpg))
#> `summarise()` ungrouping output (override with `.groups` argument)
mtcars2 <- mtcars %>%
group_by(cyl) %>%
summarise(y_05 = quantile(mpg, probs = .05))
#> `summarise()` ungrouping output (override with `.groups` argument)
mtcars3 <- mtcars %>%
group_by(cyl) %>%
summarise(y_95 = quantile(mpg, probs = .95))
#> `summarise()` ungrouping output (override with `.groups` argument)
mtcars_join <- left_join(mtcars1, mtcars2, by = "cyl") %>%
left_join(mtcars3, by = "cyl")
ggplot(mtcars_join, aes(cyl)) +
geom_ribbon(aes(ymin = y_med, ymax = y_95), fill = "red") +
geom_ribbon(aes(ymin = y_05, ymax = y_med), fill = "blue") +
geom_line(aes(y = y_med)) +
geom_line(aes(y = y_05), linetype = "dotted") +
geom_line(aes(y = y_95), linetype = "dotted")
由reprex 包(v0.3.0)于 2020-06-14 创建