首页 > 解决方案 > fun.y 和 stat_summary 停止在 ggplot 中工作

问题描述

我曾经一直运行此代码以在我的条形图顶部获得“n”的总和。

现在我收到以下错误:

忽略未知参数:fun.y

未提供汇总函数,默认为mean_se()

count %>%
ggplot(aes(x = date, y = n, group = class, fill = class)) +
  geom_col() +
  geom_text_repel(
    aes(label = stat(y), group = date), 
    stat = 'summary', fun.y = sum, vjust = -1
  ) 

ggplot 不再“汇总”数据并忽略 vjust

标签: rggplot2

解决方案


这是ggplot2release的 API 的变化ggplot2 3.3.0。从文档:

fun.ymin、fun.y、fun.ymax 已弃用,请改用上面指定的版本。

只需切换到fun.

library(ggrepel)
library(ggplot2)
library(dplyr)

mtcars %>%
  count(gear, cyl) %>% 
  ggplot(aes(x = factor(gear), y = n, group = factor(cyl), fill = factor(cyl))) +
  geom_col() +
  geom_text_repel(
    aes(label = stat(y), group = factor(gear)), 
    stat = 'summary', fun = sum, vjust = -1
  ) 

reprex 包(v0.3.0)于 2020-04-14 创建


推荐阅读