在这种情况下,我的 ggplot2 代码中 y 的 geom_text 美学应该是什么?

What should be my geom_text aesthetics for y in my ggplot2 codes in this context?

我在 RStudio 中有以下 ggplot2 代码 运行:

p2 <- ggplot(subset(df1,LEVEL %in% c("FM")),aes(x=los_group)) +
    ggtitle("FM: % of Respondents by Length of Service") + xlab("LOS Group") +
    geom_bar(aes(y = 100*(..count..)/sum(..count..)), width = 0.5, fill="steelblue") + 
    ylab("Percentage") +
    geom_text(position=position_dodge(width=0.9), hjust= 1.5, 
    vjust=0.5, angle = 90, color="white", fontface="bold", size=6)+
    coord_flip() + 
    theme_minimal()

p2

我想在条形图内(顶端)插入百分比值(带有 % 符号)。我坚持在我的 geom_text 代码中指定 y 美学。

目前,我收到以下错误消息:

Error: geom_text requires the following missing aesthetics: y, label

这是一个使用标准数据的示例,因为我们没有您的 df1

当我在多个地方使用计算时,通常最简单的方法是在 ggplot 之前计算它并将其通过管道输入,它将被解释为 ggplot 的第一个(即数据)项打电话。

library(tidyverse)

# Calculate share of counts per cylinder, and pipe that into ggplot
mtcars %>%
  count(cyl = as.factor(cyl)) %>%
  mutate(share = n / sum(n)) %>%

  # Declare global x, y, and label mapping. If not specifically specified
  #   in subsequent `geoms`, they will adopt these. 
  ggplot(aes(x=cyl, y = share, label = scales::percent(share, accuracy = 1))) +
  ggtitle("FM % of Respondents by Length of Service") + 
  xlab("LOS Group") +
  geom_col(width = 0.8, fill="steelblue") + 
  ylab("Percentage") +
  geom_text(position=position_dodge(width=0.9), hjust= 0.5, 
            vjust=-0.5, angle = 90, color="white", fontface="bold", size=6) +
  coord_flip() + 
  theme_minimal()