将标签添加到条形图的底部

Adding labels to the bottom of a bar plot

我已使用代码

将 n= 标签添加到下图中
  geom_text(angle = 0, nudge_y = -0.02, col = "#d3d3d3")

我想让 n= 标签位于条形图的底部,这样它们就不会干扰误差条形图。当我更改

 nudge_y = 

它将所有标签向下移动相同的量。如何让标签在条形底部对齐?

当使用 geom_col 绘制条形时,可以通过将 geom_text 中的 y 美学设置为(接近)零(+ 可选微调)将标签放在底部。尝试使用示例数据 mtcars:

library(dplyr)
library(ggplot2)
mtcars %>% 
  # Add count
  count(cyl, gear) %>% 
  # Add label
  mutate(label = paste("n =", n)) %>% 
  # Plot
  ggplot(aes(x = factor(cyl), y = n, fill = factor(cyl))) +
  geom_col() +
  geom_text(aes(y = 0, label = label), vjust = 0, nudge_y = .2) + 
  facet_wrap(~gear, scales = "free_x")

reprex package (v0.3.0)

于 2020 年 3 月 10 日创建