ggplot2 figure alignment

85 views
Skip to first unread message

Xiaoyu Yan

unread,
Feb 15, 2012, 11:07:31 PM2/15/12
to ggplot2
Hello there,

I am trying to align my ggplot2 figures based on their x-axis. I am using the following code, which allows me to put three figures into larger 3 row and 1 column figure. The thing is that they have same x-axis and different y axis. So, the x-axis is not perfect aligned due to that the y-axis have different number or unit, etc. The example is attached. I can adjust the size of the label to squeeze the figure so that their x-axis can be aligned, but the figure looks weird. I would like to know how to do it. Thanks a lot.


vp.layout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y)
arrange <- function(..., nrow=NULL, ncol=NULL, as.table=FALSE) {
 dots <- list(...)
 n <- length(dots)
 if(is.null(nrow) & is.null(ncol)) { nrow = floor(n/2) ; ncol = ceiling(n/nrow)}
 if(is.null(nrow)) { nrow = ceiling(n/ncol)}
 if(is.null(ncol)) { ncol = ceiling(n/nrow)}
        ## NOTE see n2mfrow in grDevices for possible alternative
grid.newpage()
pushViewport(viewport(layout=grid.layout(nrow,ncol) ) )
 ii.p <- 1
 for(ii.row in seq(1, nrow)){
 ii.table.row <- ii.row
 if(as.table) {ii.table.row <- nrow - ii.table.row + 1}
  for(ii.col in seq(1, ncol)){
   ii.table <- ii.p
   if(ii.p > n) break
   print(dots[[ii.table]], vp=vp.layout(ii.table.row, ii.col))
   ii.p <- ii.p + 1
  }
 }
}

png("Hematology.png", width=640, height=1440)
arrange(p3,p4,p5,nrow=3,ncol=1)
dev.off()
 
Hematology.png

Tengfei Yin

unread,
Feb 16, 2012, 1:10:42 AM2/16/12
to Xiaoyu Yan, ggplot2
Hi Xiaoyu,

I have a function called align.plots() previously developed by Baptiste in ggExtra package, then I made some tweak to allow alignments with legend and faceting in ggplot2 0.9. it's still under development,  but it should work now

https://github.com/tengfei/ggbio/blob/master/R/tracks.R#L78

you don't have to install my package.

just load ggplot2 and grid
library(ggplot2)
library(grid)

## load align.plots function

## do something like

align.plots(p1, p2, p3)

Please let me know if it doesn't work.

cheers

Tengfei

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2



--
Tengfei Yin
MCDB PhD student
1620 Howe Hall, 2274,
Iowa State University
Ames, IA,50011-2274



Marsh

unread,
Apr 3, 2012, 11:01:21 AM4/3/12
to ggplot2
I'm not sure to what extent this topic is related, but I figured
anybody who can solve this problem probably has the knowledge to solve
mine.

In my case, I am not generating all of the images in the same R script
and in many cases they do not appear on the same pages in the LaTeX
document I'm putting them. But I did still want to be consistent so
I've set the size of the Tikz output to be the same for all of my
plots. This is roughly OK in many cases, but I have different axis
numbers and the legend on the right is also not always the same. This
means that the actual plot region is not always quite the same width.
Also the horizontal centering is based on the entire Tikz output but
optically it would probably look better if I could center around the
plot itself. Is there any good way to produce an output that is
centered around a fixed-width plot region with the legend and axis
labels being drawn with respect to that? If I could do that, then my
LaTeX would be consistent.

Thanks,
Eric

On Feb 16, 8:10 am, Tengfei Yin <yinteng...@gmail.com> wrote:
> Hi Xiaoyu,
>
> I have a function called align.plots() previously developed by Baptiste in
> ggExtra package, then I made some tweak to allow alignments with legend and
> faceting in ggplot2 0.9. it's still under development,  but it should work
> now
>
> https://github.com/tengfei/ggbio/blob/master/R/tracks.R#L78
>
> you don't have to install my package.
>
> just load ggplot2 and grid
> library(ggplot2)
> library(grid)
>
> ## load align.plots function
>
> ## do something like
>
> align.plots(p1, p2, p3)
>
> Please let me know if it doesn't work.
>
> cheers
>
> Tengfei
>
>
>
>
>
> On Wed, Feb 15, 2012 at 10:07 PM, Xiaoyu Yan <yanxiaoyu...@gmail.com> wrote:
> > **
Reply all
Reply to author
Forward
0 new messages