gpt4 book ai didi

r - 使用ggplot多线和居中对齐x轴标签

转载 作者:行者123 更新时间:2023-12-04 01:15:55 27 4
gpt4 key购买 nike

<分区>

我知道hjust用于 x title 轴,但我将如何将 x 轴 labels 居中和多行排列?这是我的绘图功能:

gg_fun<-function(){
ggplot(tab,
aes(x = Var1, y = Percent)) +
#theme_light() +
theme(panel.background = element_rect(fill = NA),
axis.title.y=element_text(angle=0, vjust=0.5, face="bold"),
axis.title.x=element_blank(),
axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 12),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
#panel.grid.minor = element_line(colour = "dark gray"),
panel.grid.major.x = element_blank() ,
# explicitly set the horizontal lines (or they will disappear too)
panel.grid.major.y = element_line(size=.1, color="dark gray" ),
axis.line = element_line(size=.1, colour = "black"),
plot.background = element_rect(colour = "black",size = 1)) +
geom_bar(stat = "Identity", fill="#5596E6") + #"cornflower" blue
ggtitle(element_blank()) +
scale_y_continuous(expand = c(0, 0), breaks = round(seq(0, 1, by = .1), digits = 2),
labels = percent(round(seq(0, 1, by = .1), digits = 2), digits = 0),
limits = c(0,.6)) #+
#scale_x_discrete()
}

这是它生成的示例图: enter image description here

我知道 n.dodge scale_x_discrete() 的参数,但这不是我要找的。我也不想简单地缩写使用 labels = abbreviate或精确指定,因为这很耗时。我也看到了例如 levels(birds$effect) <- gsub(" ", "\n", levels(birds$effect)) , 但这会跳过每一行并使一些标签太长。我将如何将 x 标签文本居中并使其多行以防止重叠?我要做什么的例子:

enter image description here

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