如何在 R ggplot 的 geom_label 框中居中文本?

LC-*_*ist 3 r ggplot2

在 R 中为我的 (gg) 绘图使用 geom_label 时,我注意到文本上方的空间比下方的空间大。如何将文本对齐到标签框的中间?

x <- data.frame(x = c("Being not", "Creative"), y = c(0.5, 1), text = c(543,12345))

g <- ggplot(data=x, aes(x, y)) + geom_bar(stat = 'identity', fill=c("red4","cornflowerblue"))

g + geom_label(aes(y = -Inf, label = text), vjust = -2)

# increasing `label.padding` here to exaggerate the white space
g + geom_label(aes(y = -Inf, label = text), vjust = -2, label.padding = unit(1, "lines")
Run Code Online (Sandbox Code Playgroud)

示例

Ric*_*loo 5

使用明确的 y 坐标来放置标签而不是 vnudge


g + geom_label(aes(y = .25, label = text), label.padding = unit(1, "lines"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

更好的是:将 y 坐标向量传递给 yaes以指定中点。

g + geom_label(aes(y = c(.25, .5) label = text), label.padding = unit(1, "lines"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明