相关疑难解决方法(0)

在ggplot2中复制(和修改)离散轴

我想将ggplot2图上的左侧Y轴复制到右侧,然后更改离散(分类)轴的刻度标签.

我已经阅读了这个问题的答案,但是在包的repo页面上可以看到,该switch_axis_position()函数已从cowplot包中删除(作者在ggplot2中引用(即将推出?)本机功能).

我在ggplot2中看到了辅助轴上的参考页面,但是该文档中的所有示例都使用scale_y_continuous而不是scale_y_discrete.事实上,当我尝试使用离散函数时,我得到错误:

Error in discrete_scale(c("y", "ymin", "ymax", "yend"), "position_d",  : 
unused argument (sec.axis = <environment>)
Run Code Online (Sandbox Code Playgroud)

无论如何用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))
Run Code Online (Sandbox Code Playgroud)

r ggplot2 tidyverse

17
推荐指数
1
解决办法
3082
查看次数

标签 统计

ggplot2 ×1

r ×1

tidyverse ×1