aligning plots with ggExtra align.plots(): problems

163 views
Skip to first unread message

Jonathan Shore

unread,
Feb 23, 2011, 5:05:56 PM2/23/11
to ggplot2
Hi,

I do many analyses on timeseries data where want to look at many different vectors with the same time axis.   Due to various restrictions in the way one can mix and match a dataset I need to be able to align multiple ggplots vertically with precise x axis alignment.

the ggExtra  align.plots() function only works in the most trivial cases (i think).   For instance consider the following plots:

Da <- rbind(
data.frame(t=seq(1,100), var=rnorm(100), type='This is a test'),
data.frame(t=seq(1,100), var=rnorm(100), type='A'))

Db <- rbind(
data.frame(t=seq(1,100), var=rnorm(100), type='A'),
data.frame(t=seq(1,100), var=rnorm(100), type='B'))

Dc <- data.frame(t=seq(1,100), star=rnorm(100), type='A')



Ga <- ggplot() + geom_line(aes(x=t,y=var,colour=type), data=Da) 
Gb1 <- ggplot() + geom_line(aes(x=t,y=var,colour=type), data=Db) 
Gb2 <- ggplot() + geom_line(aes(x=t,y=var,colour=type), data=Db) + facet_grid(type ~ .)

Gc <- ggplot() + geom_line(aes(x=t,y=star, colour=type), data=Dc)

library(ggExtra)
align.plots (Gc, Ga)  # this is not aligned due to diff in legend width
align.plots (Gc, Gb1) # this aligns as legend width ~same
align.plots (Gc, Gb2) # this does not align as facet decoration not accounted for

Only 1 of the above plots aligns correctly.    Looking at the code, the function is:

- grabbing legend widths and axis text widths
- determining the maximum horiz extent of each of these
- redering each graph in a viewport shifted and resized against delta to the above

The code does not account for facet decoration or horizontal spacers.   Also there appears to be a rendering bug with:

 align.plots (Gc, Ga)

where the legend grob is rendered twice? and one is partially truncated  (perhaps this is only in my versions of packages?).  I did:

grid.ls(viewports=T, grob=T)

But could not determine which grobs were associated with facet labels or which would indicate additional horizontal spacing.

Pointers appreciated.

Thanks

Jonathan
-- 
Systematic Trading LLC




Jonathan Shore

unread,
Feb 23, 2011, 5:23:46 PM2/23/11
to ggplot2
Just a note, with a package update the other problems disappeared, the only problem remaining is how to adjust ggExtra align.plots() to account for possible facet decoration.

baptiste auguie

unread,
Feb 23, 2011, 9:13:56 PM2/23/11
to Jonathan Shore, ggplot2
Hi,

Indeed, align.plots() is just a bag of tricks I wrote to extract the
total width of ggplot elements and align the plot panels accordingly.
I only considered the most trivial case (no facetting, no fancy
options), mainly because I believe a real solution should be
integrated in the core of ggplot2 (and I think Hadley has been working
on it with a new approach). That said, it might be possible to adapt
align.plots() in the meantime, if you can figure out which sizes are
missing in the calculation.


baptiste

> --
> 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
>

Jonathan Shore

unread,
Feb 24, 2011, 7:05:14 AM2/24/11
to baptiste auguie, ggplot2
Thanks.  I agree that the best solution lies within ggplot itself.    I did a hack where i located that # of y axis titles as in:   

getGrob(.g,"axis.text.y.text",grep=TRUE, global=TRUE)

If the # of grobs found > 1, assumed was vertically facetted and added in an estimate of the decoration size (I could not find the grob for it) to be 15pt.   With different fonts or other options, obviously would not work.


Jonathan
Reply all
Reply to author
Forward
0 new messages