添加一个辅y轴到ggplot2图 - 使它完美 [英] add a secondary y axis to ggplot2 plots - make it perfect

查看:156
本文介绍了添加一个辅y轴到ggplot2图 - 使它完美的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加第二个y轴,缩放其中一个原始y轴。这个话题并不新鲜。它已被触及的时代,例如在此ggplot2谷歌组群线程上。按照Hadley的建议,我试图通过 geom_vline geom_segment 添加辅助y轴, geom_text 。但是,它仍然很难看。

Adding a secondary y axis, scaled one of the original y axis. This topic is not new. It has been touched times, for example on this ggplot2 google groups thread. Following Hadley's advice, I tried to add the secondary y axis by geom_vline, geom_segment, and geom_text. But, it is still ugly.

所以我会请你帮忙完成。我认为很多ggplot2用户会对这个话题感兴趣,并且更喜欢你的专业知识或贡献。提前致谢。

So I would ask for your help on making it perfect. I think many ggplot2 users would be interested in this topic and prefer any your expertise or contributions. Thanks in advance.

#########################################
# what I have gotten.
library(ggplot2)

# build up a box plot
p <- ggplot(mtcars, aes(factor(cyl), mpg)) 

# add the secondary y axis on right side of the plot
p + geom_boxplot() + geom_vline(xintercept = 3.5) + 
 geom_segment(aes(x=3.49, y=c(7,14,21,28), xend = 3.52, yend = c(7,14,21,28))) +
 geom_text(aes(x=3.55, y=c(7,14,21,28), label=c(7,14,21,28)))


推荐答案

为了避免黑客入侵,你可以使用 facet_grid 来代替。根据您的数据,您可以自定义它,使其成为更一般的辅助轴。

To avoid hacking, you might use facet_grid instead. Depending on your data, you can customize it pretty well, to make it into more general secondary axis.

 library(ggplot2)
 ggplot(mtcars, aes(factor(cyl), mpg)) + 
   geom_boxplot() + 
   facet_grid(cyl ~., scales = "free")

这篇关于添加一个辅y轴到ggplot2图 - 使它完美的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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