gpt4 book ai didi

旋转辅助轴标签的文本

转载 作者:行者123 更新时间:2023-12-02 07:46:10 24 4
gpt4 key购买 nike

我正在利用 ggplot2 中最近添加的辅助轴标签功能。我想旋转辅助轴,但无法找到文档或弄清楚如何执行此操作。

使用...旋转所有文本非常简单

ggplot(mtcars, aes(x = wt, y = mpg, colour = mpg)) +
geom_point() +
scale_x_continuous(name = 'Bottom Axis',
sec.axis = sec_axis(trans = ~ .,
name = 'Top Axis',
breaks = c(2:5),
labels = c('Two Two', 'Three Three Three', 'Four Four Four Four', 'Five Five Five Five Five'))) +
## Rotate text of x-axis
theme(axis.text.x = element_text(angle = 90))

Example dual Axis plot with both axes labels rotated我读过的任何文档(例如 scale_continuousthemes )都没有提到如何实现仅一个轴的旋转。

我要求这样做的动机是,我希望应用于数据的一些标签在水平放置时很长并且重叠,通过旋转它们我可以避免这种情况,但我希望保留底部轴上的水平方向。

最佳答案

只要您有相对最新版本的ggplot2,就可以使用axis.text.x.top:

ggplot(mtcars, aes(x = wt, y = mpg, colour = mpg)) +
geom_point() +
scale_x_continuous(
name = 'Bottom Axis',
sec.axis = sec_axis(
trans = ~ .,
name = 'Top Axis',
breaks = 2:5,
labels = c('Two Two', 'Three Three Three', 'Four Four Four Four', 'Five Five Five Five Five')
)
) +
## Rotate text of x-axis
theme(axis.text.x.top = element_text(angle = 45, hjust = 0))

enter image description here

关于旋转辅助轴标签的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118517/

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