Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 & facet_wrap - eliminate vertical distance between facets

Tags:

plot

r

ggplot2

I'm working with some data that I want to display as a nxn grid of plots. Edit: To be more clear, there's 21 categories in my data. I want to facet by category, and have those 21 plots in a 5 x 5 square grid (where the orphan is by itself on the fifth row). Thus facet_wrap instead of facet_grid.

I've got the following code written up for doing it (using the good old iris data set for my reproducible example):

library(ggplot2)
library(grid)

cust_theme <- theme_bw() + theme(legend.position="none", 
              axis.title = element_blank(), axis.ticks = element_blank(), 
              axis.text = element_blank(), strip.text = element_blank(), 
              strip.background = element_blank(), panel.margin = unit(0, "lines"), 
              panel.border = element_rect(size = 0.25, color = "black"), 
              panel.grid = element_blank())

iris.plot <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
             geom_point() + cust_theme + facet_wrap( ~ Species, ncol = 2) + 
             labs(title = "Irises by species")

This gives me ALMOST what I want, but not quite:

Plot of iris data set per above code.

I've still got a tiny strip of space between the top row of plots and the bottom row. I'd like to get rid of that entirely, but panel.margin is obviously not doing it. Is there a way to do this?

like image 220
plagueheart Avatar asked Dec 01 '22 00:12

plagueheart


1 Answers

This might be a little late, but panel.marginis now deprecated. Inside theme use panel.spacing. To eliminate the spacing between the facets then load the grid package and use panel.spacing = unit(0, "lines")

like image 107
TheSciGuy Avatar answered Dec 04 '22 13:12

TheSciGuy