gpt4 book ai didi

r - geom_text() 的边界位置

转载 作者:行者123 更新时间:2023-12-05 03:53:32 29 4
gpt4 key购买 nike

我正在制作多个倾斜条形图的实例。由于计数的大小和百分比的差异不同,在某些情况下,其中一个标签(计数)的一部分被推到栏外。在所有情况下,我都需要标签完全位于栏内。如果不重新定位以适合栏内,我需要标签按原样居中。

enter image description here

代码是:

library(tidyverse)
library(ggplot2)

data <- tibble(type = c('Cat', 'Dog'),
group = c('Pets', 'Pets'),
count = c(10000, 990000),
percent = c(1, 99))

ggplot(data, aes(x = group, y = percent, fill = type)) +
geom_bar(stat = 'identity',
position = position_stack(reverse = TRUE)) +
coord_flip() +
geom_text(aes(label = count),
position = position_stack(vjust = 0.5,
reverse = TRUE))

最佳答案

使用hjust="inward":

ggplot(data, aes(x = group, y = percent, fill = type)) +
geom_bar(stat = 'identity', position = position_stack(reverse = TRUE)) +
coord_flip() +
geom_text(aes(label = count), hjust = "inward", position = position_stack(vjust = 0.5, reverse = TRUE))

enter image description here

关于r - geom_text() 的边界位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61509824/

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