1

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())
user1723765
  • 6,179
  • 18
  • 57
  • 85
upabove
  • 1,057
  • 3
  • 18
  • 29
  • I'm not exactly clear on what you're after, but you can draw each figure individually and put it together using `gridExtra::grid.arrange`. – Roman Luštrik Feb 24 '14 at 12:52
  • yes but then since the first plot has a y-axis title and the other's don't the first plot will be squeezed to hold the same amount of space as the other three plots. – upabove Feb 24 '14 at 12:53
  • https://dl.dropboxusercontent.com/u/22681355/Rplot.pdf this is how the current plot looks like. I would like to draw the y-axis for all four plots – upabove Feb 24 '14 at 12:57
  • Re-visit this..."I'm not exactly clear on what you're after, but you can draw each figure individually and put it together using `gridExtra::grid.arrange`" (Luštrik, R, 2014). See `theme` to do the added step of removal of labels and axis title. – Tyler Rinker Feb 24 '14 at 14:52
  • As I said before, if I used grid.arrange the plots won't be of equal size. The reason is that plot1 has a y-axis and the rest don't so since grid.arrange allocated equal space to each plot, plot1 will look smaller!! For your second comment: I am already using theme() but I can only specify 1 theme() for the plot not for every single plot that I use facet_grid() on. – user1723765 Feb 24 '14 at 15:10
  • As a quick fix you could add a line via geom_segment for each facet y-axis. This wouldnt have the ticks but if you retain the panel.grid it might be ok? – user20650 Feb 24 '14 at 15:20
  • retaining the panel grid doesn't produce the y-axis. I only need the y-axis and the ticks. is this not possible to do? – user1723765 Feb 24 '14 at 15:26
  • panel.grid does not produce the y-axis. Use geom_segment to create a new line to represent the y-axis for each facet - but it won't have ticks. grid.extra is a way to go - when specifying the plots keep the attributes of each plot the same - this will keep the plots the same size when you arrange them – user20650 Feb 24 '14 at 15:40
  • it won't keep the size the same, because plot1 has additional text on the left side! – user1723765 Feb 24 '14 at 15:41
  • Keep plot attributes the same on the other plots - just tweak them. ie theme(axis.text.y=element_text(colour="white"), axis.title.y=element_text(colour="white")). So they are still thee but not visible. – user20650 Feb 24 '14 at 15:49
  • Try: facet_wrap(scales="free") – TYZ Feb 24 '14 at 15:51
  • @user20650 then there will be big distances between the plots. – user1723765 Feb 24 '14 at 16:20
  • Use the theme plot.margin ie. plot.margin = unit(c(0.5,0.1,0,0), "cm") to reduce the whitespace around the plots – user20650 Feb 24 '14 at 16:21
  • @YilunZhang that will also add the numbers to the axis – user1723765 Feb 24 '14 at 16:22
  • @user20650 if you do what you suggest, won't plot1 be wider? (since it has additional text, and the others don't) – user1723765 Feb 24 '14 at 16:23
  • Sandy Muspratt answered the question [here](http://stackoverflow.com/a/37624328/3410778) Works perfectly! – Nightingale Jun 06 '16 at 09:22

1 Answers1

3

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)
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • I get this error: Error in gtable_add_grob(g, grobs = list(axis, axis), name = "ticks", : Not all inputs have either length 1 or same length same as 'grobs', – user1723765 Feb 24 '14 at 18:20
  • any ideas why I'm getting this? – user1723765 Feb 25 '14 at 11:43
  • i have no idea what you're talking about. What edit? What's "this"? – baptiste Feb 25 '14 at 12:28
  • I used your code to extract the y-axis from the grob, but I got the error that I pasted in my first comment. The question is, why do I get this error? (Yesterday I edited the main question to insert the output from ggplotGrob() but someone removed it) – user1723765 Feb 25 '14 at 12:31
  • works fine for me, i don't know what can be causing the problem – baptiste Feb 25 '14 at 12:35
  • is there something in your code that is specfic to some kind of plot? it should work for any ggplot2 object right? – user1723765 Feb 25 '14 at 12:37
  • do you get this error with the exact code that you posted here, or with another plot? if it's the latter, you need to rethink your strategy for asking questions on SO: such a high comment rate is never a good sign. – baptiste Feb 25 '14 at 12:50
  • I created a reproducible example that's what they always ask for here. The data is different in my plot but I am using the same code to plot the figure. – user1723765 Feb 25 '14 at 12:52
  • My final question to clarify this: does your code depend on the number of facets? if yes, how can I modify the code to have 4 facets? – user1723765 Feb 25 '14 at 12:54