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:
- 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:
where the legend grob is rendered twice? and one is partially truncated (perhaps this is only in my versions of packages?). I did:
But could not determine which grobs were associated with facet labels or which would indicate additional horizontal spacing.
Pointers appreciated.