Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

r - Display y-axis for each subplot when faceting

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

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())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...