Essentially, I wish to replicate the facet_grid() effect, but have some columns have their own scales. To demonstrate, the following code is close to what I want:
library(ggplot2)
library(reshape2)
library(gridExtra)
set.seed(65421)
gg.df1 <- data.frame(x = c(1:20, 1:20),
A = sort(rnorm(40)),
B = sort(rnorm(40))+0.5,
C = sort(rnorm(40))+1,
y_lab = c(rep('1', 20), rep('2', 20)))
gg.df1 <- melt(gg.df1, id.vars=c("x", "y_lab"))
gg.df2 <- data.frame(x = c(1:20, 1:20),
D = abs(sort(rnorm(40))*200),
E = abs(sort(rnorm(40))*205),
y_lab = c(rep('1', 20), rep('2', 20)))
gg.df2 <- melt(gg.df2, id.vars=c("x", "y_lab"))
p1 <- ggplot(gg.df1, aes(x, value)) + geom_line() + facet_grid(y_lab ~ variable) + theme(strip.text.y=element_blank(), strip.background=element_rect(fill=NA, colour=NA))
p2 <- ggplot(gg.df2, aes(x, value)) + geom_line() + facet_grid(y_lab ~ variable) + labs(y=NULL) + theme(strip.background=element_rect(fill=NA, colour=NA))
grid.arrange(p1, p2, nrow=1, widths=c(3, 2))
(I've colored the facet strip to be white so it is easy to have only one set of labels.)
Note that all the facets have identical x-axes (this is true in the real data). However, the sizes of the facet regions are different between the two subplots, due to grid.arrange() and the nature of the subplots interacting with the widths argument. Is there a good way to guarantee that the facets will be the same size in grid.arrange(), or to replicate this effect using other commands?