gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:03:01 24 4
gpt4 key购买 nike

在 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")

Illustrative Sample

最佳答案

使用明确的 y 坐标代替 vnudge

放置标签
g + geom_label(aes(y = .25, label = text), label.padding = unit(1, "lines"))

enter image description here

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

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

enter image description here

关于r - 如何在 R ggplot 的 geom_label 框中将文本居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46063437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com