在条形图的底部添加标签

时间:2020-03-09 23:56:23

标签: ggplot2 geom-text labeling

enter image description here

我已使用代码

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

我想在条形图的底部放置n =标签,以免它们干扰错误条形图。当我更改

 nudge_y = 

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

1 个答案:

答案 0 :(得分:0)

使用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-03-10创建

相关问题