在ggplot2图中添加辅助y轴 - 使其完美

jia*_*mao 12 plot r ggplot2

添加辅助y轴,缩放原始y轴之一.这个话题并不新鲜.它已被触及时间,例如在这个ggplot2 google groups线程上.继哈德利的意见,我试图通过添加辅助y轴geom_vline,geom_segmentgeom_text.但是,它仍然很难看.

所以我会请求你帮助完善它.我想很多ggplot2用户会对这个主题感兴趣并且更喜欢你的专业知识或贡献.提前致谢.

#########################################
# 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)))
Run Code Online (Sandbox Code Playgroud)

Gee*_*cid 2

为了避免黑客攻击,您可以facet_grid改为使用。根据您的数据,您可以很好地对其进行自定义,以使其成为更通用的辅助轴。

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

在此输入图像描述