R 有趣。在ggplot中未识别y

R 有趣。在ggplot中未识别y,r,ggplot2,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)) 只需将geom\u line替

我正在尝试使用
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))

只需将
geom\u line
替换为
stat\u 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")
它也会告诉你乐趣。y不受欢迎,所以我只是用了乐趣

根据图例的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")
geom_-line(stat='summary'
更改为
stat_-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")

DOB不是一个因素,对吗?如果是数字,下面的解决方案应该有效谢谢你,biomiha。这非常有效!!再次感谢。有没有办法为两行添加标签?在这种情况下,我建议你将摘要统计数据作为单独的列添加。我已经编辑了我的答案,添加了图例。
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")