gpt4 book ai didi

在 ggplot2 中右对齐旋转轴标题

转载 作者:行者123 更新时间:2023-12-04 19:52:01 26 4
gpt4 key购买 nike

如何将 Y 轴标题(“物种”)与轴标签(三个物种名称)右对齐,使轴标题靠近灰色面板? hjust好像不影响位置。

library(ggplot2)

ggplot(iris,
aes(x = Species,
y = Sepal.Width)) +
geom_boxplot() +
labs(x = "Species",
y = "Sepal Width") +
coord_flip() +
theme(axis.title.y = element_text(angle = 0, hjust = 0))

enter image description here

最佳答案

您可以使用 geom_text连同clip = "off"coord_flip()这将允许在绘图面板之外绘制绘图元素。显然,您将不得不玩弄xy使用此手动方法获得所需的输出

library(ggplot2)

p <- ggplot(iris,
aes(x = Species,
y = Sepal.Width)) +
geom_boxplot() +
labs(x = NULL,
y = "Sepal Width") +
coord_flip(clip = "off") + # add clip = off here
theme(axis.title.y = element_text(angle = 0, hjust = 0))

p +
# add axis title here
geom_text(
x = 3.5,
y = 1.85,
inherit.aes = FALSE,
label = "Species",
check_overlap = TRUE,
hjust = 1,
fontface = 'bold',
size = 5
) +
theme(plot.margin = unit(c(1, 1, 1, 2), "lines"))



创建于 2018-10-27 由 reprex package (v0.2.1)

关于在 ggplot2 中右对齐旋转轴标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53027195/

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