在刻面时显示每个子图的y轴 [英] Display y-axis for each subplot when faceting

查看:102
本文介绍了在刻面时显示每个子图的y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用facet_grid来绘制三个彼此相邻的图。

I am plotting three plots next to each other using facet_grid.

方格网格将仅绘制第一个图的y轴。有没有一种方法可以绘制所有三个图的y轴?

Facet grid will only draw the y-axis for the first plot. Is there a way to draw the y-axis for all three plots?

这里有一个可重现的例子:

here's a reproducible example:

require(ggplot2)
require(reshape)
require(grid)
a<-rnorm(100)
b<-runif(100)
c<-rpois(100,lambda=2)

abc<-cbind(a,b,c)
colnames(abc)<-c("a","b","c")
abc<-melt(abc,id.vars=1:1)
colnames(abc)<-c("c","variable","value")
d<-rep(c("a","b","c"),each=100)
abc<-cbind(d,abc)
colnames(abc)<-c("cond","c","variable","value")
plot1<-ggplot(abc,aes(x=c,y=value,colour=variable,size=variable))+geom_point()+theme(legend.position="right")+facet_grid(~cond)+theme_bw()+theme(axis.text=element_text(size=8),
          axis.title=element_text(size=8),
          text = element_text(size=14),
          axis.line = element_line(size=0.25),
          axis.ticks=element_line(size=0.25),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(),
          panel.background = element_blank(),
          legend.position="none" ,
          legend.direction="vertical", 
          legend.title=element_blank(),
          legend.text=element_text(size=8), 
          plot.margin=unit(c(0,0.3,0,0),"cm"),
          legend.background=element_blank(), 
          legend.key=element_blank())


推荐答案

您可以复制(部分)y轴并为每个面板放置副本,

You can copy the (part of the) y-axis and place copies for each panel,

g <- ggplotGrob(plot1)

require(gtable)
axis <- gtable_filter(g, "axis-l")[["grobs"]][[1]][["children"]][["axis"]][,2]
segment <- segmentsGrob(1,0,1,1)
panels <- subset(g$layout, name == "panel")
g <- gtable_add_grob(g, grobs=list(axis, axis), name="ticks",
                     t = unique(panels$t), l=tail(panels$l, -1)-1)

g <- gtable_add_grob(g, grobs=list(segmentsGrob(1,0,1,1), 
                                   segmentsGrob(1,0,1,1)), 
                     t = unique(panels$t), l=tail(panels$l, -1)-1, 
                     name="segments")
grid.newpage()
grid.draw(g)

这篇关于在刻面时显示每个子图的y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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