How to remove all white space from and arrange a panel plot in R with two facet labels

365 views Asked by At

I am struggling to generate a plot that has 9 subcategories in 2 categories and that should fit on one half of an A4 page.

I have tried to use two facet labels, but the arrangement of the subplots becomes a problem and not every subplot needs to be labeled. I have tried facet, grid.arrange, ggarrange, and cowplot, but I have not managed to remove the white space between subplots and control the labeling.

Any advice on how to approach this would be great; I really want to avoid putting it in a paint program and doing it by hand (non-reproducible).

I am trying to generate a plot that looks something like this:

enter image description here

But I am using this:

inter_plots <- function(data, x_id, y_id, xlab, ylab, xmin, xmax){
  ggplot(data, aes_string(x_id, y_id, fill = "probs")) +
    geom_tile(color = "white", lwd = 0, linetype = 1) +
    scale_x_continuous(expand = c(0, 0), breaks = c(-0.5, 0, 1, 2)) +
    scale_y_continuous(expand = c(0, 0), 
                       breaks = c(min(data$sqrt_ud),8.867e-07,1.755e-06), 
                       labels = my_labels) +
    labs(x = "", y = "", fill = "Selection \nprobability") +
    scale_fill_gradientn("Selection \nprobability", colors = colfunc(135)) +
    theme_classic() +
    theme(axis.text = element_text(color = "black", size = 15),
          axis.line = element_line(linewidth = 1.2),
          text = element_text(size = 20),
          legend.text = element_text(size = 15),
          legend.position = "none",
          aspect.ratio = 1,
          axis.title.x=element_text(color = "white"),
          axis.text.x=element_text(color = "white"),
          axis.ticks.x=element_blank(),
          plot.margin=unit(c(-1,1,-1,0), "cm"))
}

to generate individual plots and arrange them in gridExtra:

gridExtra::grid.arrange(fall_plots[[1]], spring_plots[[1]], fall_plots[[2]], spring_plots[[2]], fall_plots[[3]], spring_plots[[3]], 
                        fall_plots[[4]], spring_plots[[4]], fall_plots[[5]], spring_plots[[5]], fall_plots[[6]], spring_plots[[6]], fall_plots[[7]], spring_plots[[7]], 
                        fall_plots[[8]], spring_plots[[8]], fall_plots[[9]], 
                        ncol = 2, nrow = 9, left = yleft, bottom = xbottom, 
                        vp = viewport(width=0.9, height=0.9))

And I get this instead: enter image description here

Thanks!

1

There are 1 answers

0
Allan Cameron On

Your problem is simply that your plotting window has too wide an aspect ratio. Because you have set your plots' aspect ratios to 1, you need to ensure that your output device exactly matches the layout you want.

Obviously, we don't have your data, but let's generate 18 random plots with a fixed aspect ratio:

plot_list <- lapply(1:18, function(i) {
  n <- sample(5, 2)
  ggplot(setNames(iris[n], c("x", "y")), aes(x, y)) +
    geom_point() +
    theme(aspect.ratio = 1)
})

gridExtra::grid.arrange(plot_list[[1]] , plot_list[[2]], plot_list[[3]],
                        plot_list[[4]] , plot_list[[5]], plot_list[[6]],
                        plot_list[[7]] , plot_list[[8]], plot_list[[9]],
                        plot_list[[10]] , plot_list[[11]], plot_list[[12]],
                        plot_list[[13]] , plot_list[[14]], plot_list[[15]],
                        plot_list[[16]] , plot_list[[17]], plot_list[[18]],
                        ncol = 2, nrow = 9,  
                        vp = viewport(width = 0.9, height = 0.9))

Now if I set my plotting window to 1488 x 1880 pixels, there has to be whitespace left over because of the fixed aspect ratio of the plots:

enter image description here

But if I change my plotting dimensions to 500 x 1880 pixels, I lose most of the excess whitespace:

enter image description here

The best way to remove whitespace altogether is to remove aspect.ratio = 1 from your ggplot code. This will result in all the whitespace being removed. You then just adjust the dimensions of your plotting window to make the individual panels appear square.