在ggplot2中复制离散轴 [英] Duplicating Discrete Axis in ggplot2

查看:139
本文介绍了在ggplot2中复制离散轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ggplot2(2.1.0.9001)的开发版提供了一个很好的速记,用于创建一个副轴,如果原始轴是连续的,它就是主轴的复制

  devtools :: install_github(hadley / ggplot2)
library(ggplot2)

ggplot(mpg,aes (displ,cyl))+
geom_point()+
scale_y_continuous(
sec.axis = dup_axis()

pre>

如何复制离散轴?

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

不要忘记发送到文件时需要 print 基于网格的图形:

  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 print grid-based graphics when sending to a file:

png()
  print(ggdraw(switch_axis_position(gpv, axis="y", keep="y")) )
dev.off()
#quartz 
#     2 

这篇关于在ggplot2中复制离散轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆