副轴标题旋转方向

Rotate direction of title of secondary axis

我需要在图表上添加一个 right-hand 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)

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

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))