Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easily add an '(all)' facet to facet_wrap in ggplot2?

Tags:

r

ggplot2

I have data that looks like this example in the facet_wrap documentation:


(source: ggplot2.org)

I would like to fill the last facet with the overall view, using all data.

Is there an easy way to add a 'total' facet with facet_wrap? It's easy to add margins to facet_grid, but that option does not exist in facet_wrap.

Note: using facet_grid is not an option if you want a quadrant as in the plot above, which requires the ncol or nrow arguments from facet_wrap.

like image 445
Fr. Avatar asked Sep 21 '13 14:09

Fr.


People also ask

What does Facet_wrap () help us do when added to a Ggplot?

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.

What is the difference between Facet_wrap and Facet_grid?

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.

What is facet in ggplot2?

The facet approach partitions a plot into a matrix of panels. Each panel shows a different subset of the data. This R tutorial describes how to split a graph using ggplot2 package. There are two main functions for faceting : facet_grid()


1 Answers

library(ggplot2)

p <- qplot(displ, hwy, data = transform(mpg, cyl = as.character(cyl)))
cyl6 <- subset(mpg, cyl == 6)
p + geom_point(data = transform(cyl6, cyl = "7"), colour = "red") +
  geom_point(data = transform(mpg, cyl = "all"), colour = "blue") +
  facet_wrap(~ cyl)

enter image description here

like image 92
Roland Avatar answered Sep 28 '22 18:09

Roland