0

I have several datasets that share the same x axis but whose y axes differs by several orders of magnitude. For this reason the labels in the y axis are of variable size and when plotting with ggplot, the x axes become misaligned. To make my plots comparable, I want all x axes to have equal length in the final plots. This is a fairly simple procedure for which facetting and grids can be used (or other options too). However, this procedure implies aggregating all plots and sending them to the same png or eps file. I have several reasons (which are beyond the scope of the post) to store each plot in its own png. To keep the plots comparable, therefore, I will have to find a way to force them to have the same space between the left border of the image and the line that marks the y axis.

My guess was that "padding" the labels with spaces so they all had the same number of character would fix the problem, but it didn't work out. I have also tried changing point.margin to no avail. Someone knows how to achieve this?

library(stringr)
library(ggplot2)
y1 <- seq(1,100,by=1)
y2 <- seq(1,1000000,by=10000)
x <- seq(1,100,by=1)
data <- data.frame(x,y1,y2)

m1 <- ggplot(data=data,aes(x=x,y=y1)) + geom_line() +
        scale_y_continuous(labels=function(label) str_pad(label,width=20,side="right"))
m11 <- m1 + theme_bw() + theme(panel.border = element_blank(), panel.grid.major= element_blank(), panel.grid.minor= element_blank(),axis.line=element_line(colour="black"),axis.text.x=element_text(size=12),axis.text.y=element_text(size=12))
m2 <- ggplot(data=data,aes(x=x,y=y2)) + geom_line() + 
        scale_y_continuous(labels=function(label) str_pad(label,width=20,side="right"))
m21 <- m2 + theme_bw() + theme(panel.border = element_blank(), panel.grid.major= element_blank(), panel.grid.minor= element_blank(),axis.line=element_line(colour="black"),axis.text.x=element_text(size=12),axis.text.y=element_text(size=12))

png("m1.png")
  m11
dev.off()
png("m2.png")
  m21
dev.off()

enter image description here enter image description here

Edit: Baptise methods

Community
  • 1
  • 1
j91
  • 451
  • 1
  • 4
  • 14
  • try searching for [`set_panel_size`](http://stackoverflow.com/questions/32580946/setting-absolute-size-of-facets-in-ggplot2) – baptiste Nov 28 '16 at 21:54
  • @baptiste I tried your suggestion, no success yet. Check the edit above. – j91 Nov 29 '16 at 09:32

1 Answers1

0

I found a solution that seems to adress the problem.

First, convert the ggplot object into gtables.

g1 <- ggplot_gtable(ggplot_build(m11))
g2 <- ggplot_gtable(ggplot_build(m21))

Then, find the largest width and use it so replace the width in the rest of the plots.

maxWidth = unit.pmax(g1$widths[2:3], g2$widths[2:3])
g1$widths[2:3] <- maxWidth
g2$widths[2:3] <- maxWidth

Finally, use grid.draw to print your plot into your png device.

png("m1.png")
grid.draw(g1)
dev.off()

png("m2.png")
grid.draw(g2)
dev.off()

enter image description here enter image description here

j91
  • 451
  • 1
  • 4
  • 14