在ggplot2中复制离散轴 [英] Duplicating Discrete Axis in ggplot2
本文介绍了在ggplot2中复制离散轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ggplot2(2.1.0.9001)的开发版提供了一个很好的速记,用于创建一个副轴,如果原始轴是连续的,它就是主轴的复制 :
devtools :: install_github(hadley / ggplot2)
pre>
library(ggplot2)
ggplot(mpg,aes (displ,cyl))+
geom_point()+
scale_y_continuous(
sec.axis = dup_axis()
)
如何复制离散轴?
ggplot( mpg,aes(displ,factor(cyl)))+
geom_point()+
...?
解决方案cowplot库有这样的功能:
library(cowplot)
gpv < - ggplot(mpg,aes(displ,factor(cyl))+
geom_point()
ggdraw(switch_axis_position(gpv,axis =y,keep =y))
不要忘记发送到文件时需要
png()
print(ggdraw(switch_axis_position(gpv,axis =y,keep =y)))
dev.off()
#quartz
#2
The development version of ggplot2 (2.1.0.9001) provides a nice shorthand for creating a secondary axis that is a duplication of the primary axis if the original axis is continuous:
devtools::install_github("hadley/ggplot2") library(ggplot2) ggplot(mpg, aes(displ, cyl)) + geom_point() + scale_y_continuous( sec.axis = dup_axis() )
How can a discrete axis be duplicated?
ggplot(mpg, aes(displ, factor(cyl))) + geom_point() + ...?
解决方案The cowplot library has that facility:
library(cowplot) gpv <- ggplot(mpg, aes(displ, factor(cyl))) + geom_point() ggdraw( switch_axis_position( gpv, axis="y", keep="y"))
Don't forget that you need to
png() print(ggdraw(switch_axis_position(gpv, axis="y", keep="y")) ) dev.off() #quartz # 2
这篇关于在ggplot2中复制离散轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文