在刻面时显示每个子图的y轴

时间:2014-02-24 12:30:14

标签: r ggplot2

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

小平面网格仅绘制第一个绘图的y轴。有没有办法为所有三个图绘制y轴?

这是一个可重现的例子:

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

1 个答案:

答案 0 :(得分:3)

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

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)