Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested facet plot with ggplot2

Tags:

r

ggplot2

If I have a nested factor, in this case I have multiple "Family" levels that are contained in the factor "Order", I would like to potentially create a

facet_grid(Family / Order ~.)

instead of the current

facet_grid(Family + Order ~.) 

Basically -- ONE strip for every Order -- that contains next to it all strips for each family inside that Order. I know that facet_grid(Family / Order ~.) is currently not possible, but how would I achieve this effect? Could it be done with a theme()? Thank you so much. --SB

I should have specified above that both Family and Order are factors. The data values B are by Species which have a Family level and Order level they belong to. Here is the code for my plot:

p <- ggplot(models, aes(B,Species)) + geom_point() + facet_grid(Family + Order ~
 .,scales="free",space="free")

Here is some sample data:

structure(list(Species = c("Acanthocyclops robustus", "Acroperus harpae", 
"Alona affinis", "Ascaphus truei", "Bosmina longirostris"), Intercept = c(-36.1182388331068, 
-27.2140776216155, -25.7920464721491, -39.2233884219763, -31.4301301084581
), B = c(0.919397836908493, 0.716601987210452, 0.685455190113372, 
1.04159758611351, 0.81077051300147), Bconf = c(0.407917065756464, 
0.181611850119198, 0.254101713856315, 0.708582768458448, 0.234313394549538
), Order = c("Cyclopoida", "Diplostraca", "Diplostraca", "Anura", 
"Diplostraca"), Family = c("Cyclopidae", "Chydoridae", "Chydoridae", 
"Leiopelmatidae", "Bosminidae")), .Names = c("Species", "Intercept", 
"B", "Bconf", "Order", "Family"), row.names = c(NA, 5L), class = "data.frame")
like image 874
user1536207 Avatar asked Nov 30 '12 19:11

user1536207


People also ask

Can you facet wrap by 2 variables?

Note that you can add as many (categorical) variables as you'd like in your facet wrap, however, this will result in a longer loading period for R.

When should you use Facet_wrap?

The simplest answer is that you should use facet_wrap() when faceting by a single variable and facet_grid() when faceting by two variables and want to create a grid of panes. facet_wrap() is most commonly used to facet by a plot by a single categorical variable.

What is the difference between Facet_grid and Facet_wrap?

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 faceting in Ggplot?

Faceting is the process that split the chart window in several small parts (a grid), and display a similar chart in each section. Each section usually shows the same graph for a specific group of the dataset. The result is usually called small multiple.


1 Answers

Using facet_grid or facet_wrap will not build the graphic you are trying to build. You can, however, build a list of graphics and then plot them via gridExtra::grid.arrange. Here is an example

library(ggplot2)
library(gridExtra)
library(dplyr)

dat <-
  structure(list(Species = c("Acanthocyclops robustus", "Acroperus harpae", 
  "Alona affinis", "Ascaphus truei", "Bosmina longirostris"), Intercept = c(-36.1182388331068, 
  -27.2140776216155, -25.7920464721491, -39.2233884219763, -31.4301301084581
  ), B = c(0.919397836908493, 0.716601987210452, 0.685455190113372, 
  1.04159758611351, 0.81077051300147), Bconf = c(0.407917065756464, 
  0.181611850119198, 0.254101713856315, 0.708582768458448, 0.234313394549538
  ), Order = c("Cyclopoida", "Diplostraca", "Diplostraca", "Anura", 
  "Diplostraca"), Family = c("Cyclopidae", "Chydoridae", "Chydoridae", 
  "Leiopelmatidae", "Bosminidae")), .Names = c("Species", "Intercept", 
  "B", "Bconf", "Order", "Family"), row.names = c(NA, 5L), class = "data.frame")

dat

# A ggplot object with NO data.  Omit the order from the facet_grid call
g <- 
  ggplot() +
  aes(Species, B) +
  geom_point() +
  facet_grid(. ~ Family,
             scales = "free", space = "free") +
  ylim(range(dat$B)) +
  xlab("")

# Build a seperate graphic for each Order and title
plots <-
  lapply(unique(dat$Order), function(o) {
           g %+% dplyr::filter_(dat, ~ Order == o) + ggtitle(o)
             })

# build as Grobs and plot via gridExtra::grid.arrange
plots %>%
  lapply(ggplotGrob) %>%
  arrangeGrob(grobs = .) %>%
  grid.arrange(., ncol = 1)

enter image description here

like image 155
Peter Avatar answered Oct 12 '22 23:10

Peter