【发布时间】:2018-01-03 20:08:38
【问题描述】:
我想将 ggplot2 图上的左侧 Y 轴复制到右侧,然后更改离散(分类)轴的刻度标签。
我已经阅读了this question 的答案,但是正如on the package's repo page 所见,switch_axis_position() 函数已从cowplot 包中删除(作者引用(即将发布?)ggplot2 中的本机功能) .
我在 ggplot2 的辅助轴上看到了 reference 页面,但是该文档中的所有示例都使用 scale_y_continuous 而不是 scale_y_discrete。而且,确实,当我尝试使用离散函数时,我得到了错误:
Error in discrete_scale(c("y", "ymin", "ymax", "yend"), "position_d", :
unused argument (sec.axis = <environment>)
有没有办法用 ggplot2 做到这一点?即使是完全破解的解决方案对我来说也足够了。提前致谢。 (以下 MRE)
library(ggplot2)
# Working continuous plot with 2 axes
ggplot(mtcars, aes(cyl, mpg)) +
geom_point() +
scale_y_continuous(sec.axis = sec_axis(~.+10))
# Working discrete plot with 1 axis
ggplot(mtcars, aes(cyl, as.factor(mpg))) +
geom_point()
# Broken discrete plot with 2 axes
ggplot(mtcars, aes(cyl, as.factor(mpg))) +
geom_point() +
scale_y_discrete(sec.axis = sec_axis(~.+10))
【问题讨论】:
-
查看
scale_y_discrete的来源,没有用于指定辅助轴的选项/参数。因此,任何解决方案都可能必须是 hack。 -
我在 ggplot 提出了一个 issue 来支持
sec.axis在scale_y/x_discrete()中的原生支持。