【发布时间】:2020-06-21 21:21:35
【问题描述】:
我已使用代码将 n= 标签添加到下图中
geom_text(angle = 0, nudge_y = -0.02, col = "#d3d3d3")
我希望 n= 标签位于条形的底部,这样它们就不会干扰误差线。当我改变
nudge_y =
它将所有标签向下移动相同的数量。如何让标签在条形底部对齐?
【问题讨论】:
标签: ggplot2 geom-text labeling
我已使用代码将 n= 标签添加到下图中
geom_text(angle = 0, nudge_y = -0.02, col = "#d3d3d3")
我希望 n= 标签位于条形的底部,这样它们就不会干扰误差线。当我改变
nudge_y =
它将所有标签向下移动相同的数量。如何让标签在条形底部对齐?
【问题讨论】:
标签: ggplot2 geom-text labeling
当使用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 创建
【讨论】: