【问题标题】:fun.y not recognized in ggplotfun.y 在 ggplot 中无法识别
【发布时间】:2023-03-11 10:02:02
【问题描述】:

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

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

未提供摘要函数,默认为mean_se()

未提供摘要函数,默认为mean_se()

警告信息:

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

我的代码如下:

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")

【问题讨论】:

  • DOB 不是一个因素,对吧?如果是数字,下面的解决方案应该可以工作

标签: r ggplot2


【解决方案1】:

只需将geom_line 替换为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")

它还会告诉你 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")

【讨论】:

  • 非常感谢,biomiha。这工作得很好!
  • 再次感谢。有没有办法给这两行添加标签?
  • 在这种情况下,我建议您将汇总统计信息添加为单独的列。我已经编辑了答案以添加图例。
【解决方案2】:

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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2021-12-07
    • 2018-03-28
    • 2022-01-17
    • 1970-01-01
    • 2020-04-22
    • 2017-05-17
    相关资源
    最近更新 更多