If I have a data.frame dat
and want to plot groups of data using facet_wrap
:
dat <- data.frame(x = runif(150), y = runif(150), z = letters[1:15])
ggplot(dat[dat$z %in% letters[1:9], ], aes(x, y)) +
geom_point() +
facet_wrap( ~ z, ncol = 3, nrow = 3)
This looks great and performs as expected. However, if I plot the next set of z
on a new plot:
ggplot(dat[dat$z %in% letters[10:15], ], aes(x, y)) +
geom_point() +
facet_wrap( ~ z, ncol = 3, nrow = 3)
I no longer have 3 rows and 3 columns. I can fix the aspect ratios of the plots using opts(aspect.ratio = 1)
but I still have them laid out differently that my previous plot. I'd like it to appear as though there are always 9 plots on the page even if there are 6 or 1. Is that possible?
facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.
facet_wrap() with two variables Compute the counts for the plot so we have two variables to use in faceting: marvel_count <- count(marvel, year, align, gender) glimpse(marvel_count) ## Observations: 155 ## Variables: 4 ## $ year <dbl> 1939, 1939, 1940, 1940, 1940, 1941, 1941, 1943, 1944, 19...
The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.
Try this,
library(ggplot2)
library(plyr)
library(gridExtra)
dat <- data.frame(x=runif(150), y=runif(150), z=letters[1:15])
plotone = function(d) ggplot(d, aes(x, y)) +
geom_point() +
ggtitle(unique(d$z))
p = plyr::dlply(dat, "z", plotone)
g = gridExtra::marrangeGrob(grobs = p, nrow=3, ncol=3)
ggsave("multipage.pdf", g)
library(cowplot)
provides a handy function plot_grid
that we can use to arrange a list of plots.
First, lets construct the list of individual plots:
p = lapply(unique(dat$z), function(i){
ggplot(dat[dat$z == i, ], aes(x, y)) +
geom_point() +
facet_wrap(~z)
})
Now we can arrange the panels using plot_grid:
plot_grid(plotlist = p[1:9], nrow = 3, ncol = 3)
plot_grid(plotlist = p[10:15], nrow = 3, ncol = 3)
For anyone coming to this late like me, you can use facet_wrap_paginate
from ggforce
to keep using ggplot2
. Help file says: "This extension to ggplot2::facet_wrap()
will allow you to split a facetted plot over multiple pages." Something like the following will work:
library(ggplot2)
library(ggforce)
data("diamonds")
ggplot(diamonds) +
geom_point(aes(carat, price), alpha = 0.1) +
facet_wrap_paginate(~clarity, ncol = 2, nrow = 2, page = 2)
Use page
to select which page you want to view or wrap in a loop to display all in a document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With