1

I am trying to alter the layout of a raster stack I plotted using ggplot. Below is a sample code to work with. Basically, I would like to move the facet labelled G (including the panel, x-axis and y-axis) into the empty facet.

library(raster)

# sample data to work with 
r <- raster(ncol=10, nrow=10, vals=1:100)
r <- stack(replicate(7, r))
names(r) <- LETTERS[1:nlayers(r)]

coords <- xyFromCell(r, seq_len(ncell(r)))
dat <- stack(as.data.frame(getValues(r)))
names(dat) <- c('value', 'variable')

dat <- cbind(coords, dat)

# Plotting the raster stack using ggplot
p <- ggplot(dat, aes(x, y, fill = value))+
  geom_raster() +
  facet_wrap(~ variable, ncol = 2, as.table = TRUE)+
  coord_equal()+
  scale_fill_gradientn(colours = rev(terrain.colors(225))) +
  theme(legend.position = 'none',
        axis.title = element_blank())
p

This post solved a similar issue but I just can't understand it enough to make it work for my case. I also looked at the (Unofficial) overview of gtable but I only get confused about the layout specification. Therefore, I would like to have the job done but I would also appreciate long explanation, particularly about the layout in gtable.

Liman
  • 1,270
  • 6
  • 12

0 Answers0