fun.y 在 ggplot 中无法识别

See*_*ema 3 r ggplot2

我正在尝试使用ggplot2.

DF<-data.frame(DOB = c(1965, 1949, 1964, 1979, 1960, 1992, 1991, 1963, 1964, 1992, 1971, 1965),
               trip.duration.hr =c(3.36, 2.25, 5.31, 10.7, 1.96, 4.33, 23.55, 3.92, 5.46, 3.45, 13.72, 7.33))
Run Code Online (Sandbox Code Playgroud)

我在下面插入了我的代码。当我尝试运行它时,它给了我以下错误消息:

未提供汇总功能,默认为 mean_se()

未提供汇总功能,默认为 mean_se()

警告信息:

1:忽略未知参数:fun.y 2:忽略未知参数:fun.y 3:删除包含非有限值(stat_summary)的 40135 行。4:删除了 40135 行包含非有限值 (stat_summary)。5:删除了 40216 行包含缺失值 (geom_point)。

我的代码如下:

ggplot(DF, aes(x=DOB, y=trip.duration.hr)) +
  geom_jitter(alpha=1/10) +
  geom_line(stat = 'summary', fun.y = "mean", color="orange", size=1) +
  geom_line(stat = 'summary', fun.y = "quantile", fun.args = list(probs = .9), linetype=2, color="red")
Run Code Online (Sandbox Code Playgroud)

bio*_*iha 6

只需替换geom_linestat_summary包括geom = "line"如下:

ggplot(DF, aes(x = DOB, y = trip.duration.hr)) +
  geom_jitter(alpha = 1/10) +
  stat_summary(geom = "line", fun = "mean", color = "orange", size = 1) +
  stat_summary(geom = "line", fun = "quantile", fun.args = list(probs = .9), linetype = 2, color = "red")
Run Code Online (Sandbox Code Playgroud)

它还会告诉你 fun.y 已被弃用,所以我只是使用 fun 来代替。

根据对图例的 OP 请求进行编辑

library(tidyverse)
DF %>% 
  group_by(DOB) %>% 
  mutate(mean = mean(trip.duration.hr),
         quantile = quantile(trip.duration.hr, probs = 0.9)) %>% 
  ungroup %>% 
  pivot_longer(cols = c(mean, quantile), names_to = "summary_stat") %>% 
  ggplot(aes(x = DOB, y = value, group = summary_stat)) +
  geom_jitter(aes(x = DOB, y = trip.duration.hr), inherit.aes = FALSE, alpha = 1/10) +
  geom_line(aes(lty = summary_stat, col = summary_stat)) +
  scale_colour_manual(values = c("orange", "red")) +
  labs(y = "trip.duration.hr")
Run Code Online (Sandbox Code Playgroud)