繁体   English   中英

副轴标题旋转方向

[英]Rotate direction of title of secondary axis

我需要在图表上放置一个额外的右侧 y 轴。 我已经使用下面的代码在 ggplot 中做到了这一点。 但是该轴的文本方向与主 y 轴的文本方向相反。 我被要求使文本方向相同。 我已经在 ggplot 帮助和在线搜索了相关说明,但在任何地方都找不到有关如何控制辅助轴标题方向的信息(勾选标签是,标题否)。

如果有人能告诉我如何做到这一点,我将不胜感激。 谢谢你。

tbbl <- tibble(ltrs = letters,
               nums = rnorm(26)) %>%
  mutate(rownum = row_number()) %>%
  mutate(colr = factor(.$rownum %% 2, levels = 0:1) )

gx <- ggplot(data = tbbl, aes(x = rownum)) +
  geom_col(aes(y = nums, colour = colr, fill = colr)) +
  scale_y_continuous(sec.axis = sec_axis(~ . / 4.184, 
                                         name = 'Long title that my client finds hard to read if aligned the default way')) +
  labs(x = 'title for x axis', y = 'title for y axis')
print(gx)

您可以通过主题选项axis.title.y.right设置辅助 y 轴标题的角度:

library(ggplot2)
library(dplyr)

tbbl <- tibble(ltrs = letters,
               nums = rnorm(26)) %>%
  mutate(rownum = row_number()) %>%
  mutate(colr = factor(.$rownum %% 2, levels = 0:1) )

ggplot(data = tbbl, aes(x = rownum)) +
  geom_col(aes(y = nums, colour = colr, fill = colr)) +
  scale_y_continuous(sec.axis = sec_axis(~ . / 4.184, 
                                         name = 'Long title that my client finds hard to read if aligned the default way')) +
  labs(x = 'title for x axis', y = 'title for y axis') +
  theme(axis.title.y.right = element_text(angle = 90))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM