I want to set the free scales for x panel using geom_tile and facet_grid in ggplot2 (see the figure below). I used a for loop and print in different viewport to create this figure.
Is there simple method to create the similar figure?
Thanks for any suggestions.
This is the example codes to recreate my figure.
df <- list(x = 1:5, y = 1:5, xp = 1:3, yp = 1:3)
df <- expand.grid(df)
df$z <- df$xp * 10 + runif(nrow(df))
library(RColorBrewer)
cols <- rev(brewer.pal(11, 'RdYlBu'))
library(ggplot2)
p <- ggplot(df)
p <- p + geom_tile(aes(x, y, fill = z))
p <- p + facet_grid(xp~yp)
p + scale_fill_gradientn(colours = cols)
xp <- unique(df$xp)
library(grid)
for (i in seq(along = xp))
{
df_i <- df[df$xp == xp[i],]
p <- ggplot(df_i)
p <- p + geom_tile(aes(x, y, fill = z))
p <- p + facet_grid(xp~yp)
p <- p + scale_fill_gradientn(colours = cols)
p <- p + theme(
plot.background = element_rect(fill = 'transparent', colour = 'transparent'),
strip.background = element_rect(fill = 'transparent', colour = 'transparent'),
axis.ticks = element_line(colour = 'transparent'))
p <- p + xlab('') + ylab('')
if (i > 1)
{
p <- p + theme(strip.text.x = element_text(colour = 'transparent'))
}
print(p, vp = viewport(0.5, 5/6 - (i - 1) * 0.28, 1, 0.4))
}