liveBook · Manning

Facet_wrap() function

As explained in Statistics Slam Dunk

The facet_wrap() function creates separate plot panels for levels of a conditioning variable.

facet_wrap() divides one ggplot visualization into separate panels defined by the levels or combinations of conditioning variables. Each panel shows the same kind of chart for a different subgroup, making comparisons easier while retaining a shared visual structure. section 2.5.3 section 9.5.2

For a single conditioning variable, use facet_wrap(~variable). If Born2 has two levels, facet_wrap(~Born2) produces two panels. In the NBA draft example, each panel contains boxplots of career win shares by college attendance, separated by players’ birthplace grouping. section 2.5.3

p2 <- ggplot(draft, aes(x = College2, y = WS)) + 
 geom_boxplot(color = "orange4", fill = "orange1") +  
 labs(title = "Career Win Shares Distribution of 
      NBA First-Round Selections",
      x = "", 
      y = "Career Win Shares", 
      subtitle = "2000-09 NBA Drafts") + 
 stat_summary(fun = mean, geom = "point", shape = 20, 
              size = 8, color = "white", fill = "white") + 
 theme(plot.title = element_text(face = "bold")) +
 facet_wrap(~Born2) + 
 scale_x_discrete(breaks = c(0, 1),
                  labels = c("No College", "College"))
print(p2)

This plot layers boxplots and mean markers, then uses facet_wrap(~Born2) to create a separate comparison panel for each birthplace level. scale_x_discrete() replaces coded college values with readable labels. section 2.5.3

With two conditioning variables, facet_wrap(teamDayOff~opptDayOff) creates a panel for each combination. Twenty-five home-team and opponent days-off permutations are arranged automatically in a 5 × 5 grid. section 9.5.2

Facet layout and scales can be adjusted when a comparison needs a different presentation:

  • Add ncol to set the number of columns; the number of rows changes to accommodate the fixed number of panels. section 9.5.2
  • Keep fixed scales to harmonize axes across panels, or set scales to free for independently scaled panel axes. section 9.5.2
  • Use xlim() to leave room for labels when bar values extend toward the edge of each panel. section 9.5.2

Comparing home and away wins across rest combinations

A horizontal bar chart maps win totals to x and Home/Away to y, then facets by teamDayOff and opptDayOff. geom_text() adds counts to the bars, while the shared x-axis lets the 25 rest-condition panels be read against one another. section 9.5.2

ggplot(data = finaldf, aes(x = n, y = teamLoc, fill = teamLoc)) +
 geom_bar(stat = "identity") +
 facet_wrap(teamDayOff~opptDayOff) +
 labs(title = 
        "Home and Away Win Totals Broken Down by Days Off Permutations", 
      subtitle = "2012-13 to 2017-18 Regular Seasons", 
      caption = "Top Numbers: Home team prior days off
      Bottom Numbers: Away team prior days off",
      x = "Win Totals",
      y = "") +
 xlim(0,1500) +
 geom_text(aes(label = n, vjust = 0.1, hjust = -0.1)) +
 theme(plot.title = element_text(face = "bold")) +
 theme(legend.position = "none")

This complete ggplot call combines geom_bar(), facet_wrap(teamDayOff~opptDayOff), xlim(), and geom_text() to display home and away win totals for every days-off permutation. section 9.5.2

How Statistics Slam Dunk covers it

How deep it goes
Explains panel creation, automatic layout, column customization, fixed versus free scales, and labeling space without describing internals.
Examples
Uses career win-share boxplots by birthplace and college attendance, plus home-and-away win bars across days-off permutations.
What you can do after
Apply it via ggplot2 to create subgroup panels, customize their column layout, and choose shared or independent scales.
How it connects
Connects facet_wrap() with ggplot(), geom_boxplot(), geom_bar(), stat_summary(), scale_x_discrete(), xlim(), and geom_text().

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.