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. 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. Could anyone help me? Thank you so much!