1

I want to plot multiple time-series figures. I want them to be aligned on the same x axis.

However, due to the different quantity of y in the different plots, it is hard to simply plot them together. Y-labels take different space.

Is there any other way to align x-axis.

grid.newpage() 
pushViewport( viewport( layout=grid.layout( 4, 1 ) ) ) 
vplayout<-function( x, y ) viewport( layout.pos.row=x, layout.pos.col=y ) 
print( plot1, vp=vplayout( 1, 1 ) )
print( plot2, vp=vplayout( 2, 1 ) ) 
print( plot3, vp=vplayout( 3, 1 ) ) 
print( plot4, vp=vplayout( 4, 1 ) ) 

enter image description here

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
zxwjames
  • 265
  • 5
  • 9
  • what package are you using for plotting? as far as I know this would be easy in base R. It would also help to have a reproducible example. – MichaelChirico Dec 30 '15 at 18:32
  • to plot multiple images, I used grid. The image is plot by ggplot. Please note, the above figure is just one image. – zxwjames Dec 30 '15 at 18:42
  • 1
    Check out this one: http://stackoverflow.com/questions/30571198/how-achieve-identical-facet-sizes-and-scales-in-several-multi-facet-ggplot2-grah/30571289#30571289 – Marat Talipov Dec 30 '15 at 20:08
  • BTW, you could simply use a scientific notation for the Y axis, which will automatically fix your problem. – Marat Talipov Dec 30 '15 at 21:18

1 Answers1

1

If you reshape the data and use facet_wrap it should work for your requirements.

library(reshape2)
library(ggplot2)
testdata<-as.data.frame(cbind(x=1:100, y1=rnorm(50), y2=100000*rnorm(50)))
testdata.melt<- melt(testdata, id.var = 'x')
ggplot(testdata.melt, aes(x = x, y = value, group = variable)) +
    geom_line() +
    facet_wrap(~ variable, ncol = 1, scales = "free_y")

The resulting graph is: enter image description here

Frank
  • 190
  • 1
  • 10