gpt4 book ai didi

r - ggplot2:无法更改位置 = 'right' 的axis.title.y 角度

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

这是一个基本的情节:

ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
geom_point() +
theme_linedraw()

enter image description here

如果我不想旋转 y 轴标签,我可以这样做:
ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
geom_point() +
theme_linedraw() +
theme(axis.title.y =
element_text(color='red', angle=0, vjust = 0.5))

enter image description here

如果我想要它在右侧,我可以这样做:
ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
geom_point() +
theme_linedraw() +
scale_y_continuous(position = 'right')

enter image description here

但是为什么我不能两者都做呢?
ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
geom_point() +
theme_linedraw() +
scale_y_continuous(position = 'right') +
theme(axis.title.y =
element_text(color='red', angle=0, vjust = 0.5))

enter image description here

以供引用:
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X El Capitan 10.11.6

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets
[6] methods base

other attached packages:
[1] magrittr_1.5 ggplot2_2.2.1

loaded via a namespace (and not attached):
[1] labeling_0.3 colorspace_1.2-6 scales_0.4.1
[4] lazyeval_0.2.0 plyr_1.8.4 tools_3.3.3
[7] gtable_0.2.0 tibble_1.3.4 yaml_2.1.13
[10] Rcpp_0.12.12.5 grid_3.3.3 digest_0.6.12
[13] rlang_0.1.2 munsell_0.4.3

最佳答案

ggplot2::theme具有适用组件的左右选项。所以你可以使用 axis.title.y.right .

奇怪的是您需要明确指定 rightaxis.title.y两种颜色leftright正如你在评论中所说,很好的发现!

library(ggplot2)
ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
geom_point() +
theme_linedraw() +
scale_y_continuous(position = 'right') +
theme(axis.title.y.right = element_text(color='red', angle=0, vjust = 0.5))

关于r - ggplot2:无法更改位置 = 'right' 的axis.title.y 角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47821951/

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