简体   繁体   English

如何使用 geom_ribbon 进行绘图?

[英]How to use geom_ribbon to plot?

I am using 'flights' data set from 'nycflights13' package and 'ggplot2' package to convert the code using stat_summary function into the one using geom_ribbon() , geom_line() , and geom_point() functions.我正在使用 'nycflights13' 包和 'ggplot2' 包中的 'flights' 数据集,将使用stat_summary函数的代码转换为使用geom_ribbon()geom_line()geom_point()函数的代码。 Here is the original code:这是原始代码:

      flights %>% select(hour, dep_delay, arr_delay) %>% filter(hour > 4) %>%
  pivot_longer(!hour) %>%
  ggplot() +
  stat_summary(aes(hour, value, color = name),
               fun = mean,
               geom = "point",
               size = 3) +
  stat_summary(aes(hour, value, color = name),
               fun = mean,
               geom = "line",
               size = 1.1) +
  stat_summary(aes(hour, value, color = name),
               fun.data = "mean_sdl",
               fun.args = list(mult = 0.2),
               geom = "ribbon",
               alpha = 0.3) +
  theme_bw()

Below is my code:下面是我的代码:

 df = flights %>%  
  select(hour, dep_delay, arr_delay) %>% filter(hour > 4) %>%
  pivot_longer(!hour) %>% group_by(hour,name) %>%
  summarise(value = mean(value, na.rm = T))
df %>% mutate(low = value - sd(value)*(0.2), high = value + sd(value)*(0.2)) %>% ggplot() +
  geom_point(aes(hour, value, color = name), size = 3) +
  geom_line(aes(hour, value, color = name), size = 1.1) +
  geom_ribbon(aes(x = hour, ymax = high, ymin = low), alpha = 0.3)
  theme_bw()

However, the plot I made is not similar to the orginal one, I know the problem lies in the geom_ribbon() part but I don't know how to fix it.但是,我制作的情节与原始情节不相似,我知道问题出在geom_ribbon()部分,但我不知道如何解决。 Could anyone help me?有人可以帮我吗? Thank you so much!太感谢了!

library(nycflights13)
library(tidyverse)
f <- flights %>% 
   select(hour, dep_delay, arr_delay) %>% 
   filter(hour > 4) %>%
   pivot_longer(!hour)

Replicate the calculation that stat_summary() does internally, applying the mean_sdl function to each hour/name combination:复制stat_summary()在内部进行的计算,将mean_sdl函数应用于每个小时/名称组合:

fs <- (f
  ## partition data
  %>% group_by(hour, name)
  ## convert value to a list-column
  %>% nest()
  ## summarise each entry
  %>% mutate(across(data, map, \(x) mean_sdl(x, mult = 0.2)))
  ## collapse back to a vector
  %>% unnest(cols = c(data))
)

Now create the plot:现在创建情节:

ggplot(fs) +
  aes(hour, y = y, ymin = ymin, ymax = ymax, color = name) +
  geom_point(size = 3) +
  geom_line(size = 1.1) +
  geom_ribbon(alpha = 0.3) + 
  theme_bw()

The order of the elements affects the colours of the lines — ie if geom_ribbon is last, it covers the lines with one or two layers of "black/alpha=0.3" (depending on whether the lines are overlapped by one or both confidence regions).元素的顺序会影响线条的颜色——即,如果geom_ribbon在最后,它会用一层或两层“黑色/alpha=0.3”覆盖线条(取决于线条是否被一个或两个置信区域重叠) . I might recommend drawing the lines and points after you draw the ribbon, so that the colours are closer to the originally specified values/more predictable (but there's no need to do that if you like the way your plot looks).我可能会建议您在绘制功能区绘制线条和点,以便颜色更接近最初指定的值/更可预测(但如果您喜欢绘图的外观,则无需这样做)。

You need to add name as a grouping variable.您需要将name添加为分组变量。 The natural way to do this is to map it to the color aesthetic:这样做的自然方法是将其映射到color美学:

 flights %>%  
  select(hour, dep_delay, arr_delay) %>% 
  filter(hour > 4) %>%
  pivot_longer(!hour) %>% 
  group_by(hour, name) %>%
  summarise(mean = mean(value, na.rm = T), 
            high = mean(value, na.rm = T) + 0.2 * sd(value, na.rm= T),
            low  = mean(value, na.rm = T) - 0.2 * sd(value, na.rm= T)) %>%
  ggplot() +
  geom_point(aes(hour, mean, color = name), size = 3) +
  geom_line(aes(hour, mean, color = name), size = 1.1) +
  geom_ribbon(aes(x = hour, ymax = high, ymin = low, color = name), alpha = 0.3) +
  theme_bw()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM