Faceting splits one data set into panels so each group can be compared separately.

Program

Play the script to switch grouping strategies and count how many panels would appear.

group_choice
facet_groups.R
Replay: real traced execution (multi-file project)
group_choice <- 2
region <- c("north", "south", "north", "east")
group <- c("region", "single", "pair")[group_choice]
facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))
panels <- length(unique(facet))
label <- paste(group, panels, sep = ":")
cat(label, "\n", sep = "")
group_choice <- 1
region <- c("north", "south", "north", "east")
group <- c("region", "single", "pair")[group_choice]
facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))
panels <- length(unique(facet))
label <- paste(group, panels, sep = ":")
cat(label, "\n", sep = "")
group_choice <- 3
region <- c("north", "south", "north", "east")
group <- c("region", "single", "pair")[group_choice]
facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))
panels <- length(unique(facet))
label <- paste(group, panels, sep = ":")
cat(label, "\n", sep = "")
  1. group_choice ← 2

    1group_choice <- 22region <- c("north", "south", "north", "east")
    values this step2group_choice
  2. region ← north, south, north, east

    1group_choice <- 22region <- c("north", "south", "north", "east")3group <- c("region", "single", "pair")[group_choice]
    values this stepnorth, south, north, eastregion
  3. group ← single

    2region <- c("north", "south", "north", "east")3group <- c("region", "single", "pair")[group_choice]4facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))
    values this stepsinglegroup2group_choice
  4. facet ← all, all, all, all

    3group <- c("region", "single", "pair")[group_choice]4facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))5panels <- length(unique(facet))
    values this stepall, all, all, allfacetsinglegroup
  5. panels ← 1

    4facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))5panels <- length(unique(facet))6label <- paste(group, panels, sep = ":")
    values this step1panelsall, all, all, allfacet
  6. label ← single:1

    5panels <- length(unique(facet))6label <- paste(group, panels, sep = ":")7cat(label, "\n", sep = "")
    values this stepsingle:1labelsinglegroup1panels
  7. cat(label, " ", sep = "")

    6label <- paste(group, panels, sep = ":")7cat(label, "\n", sep = "")
    outputsingle:1
    values this stepsingle:1label
  1. group_choice ← 1

    1group_choice <- 12region <- c("north", "south", "north", "east")
    values this step1group_choice
  2. region ← north, south, north, east

    1group_choice <- 12region <- c("north", "south", "north", "east")3group <- c("region", "single", "pair")[group_choice]
    values this stepnorth, south, north, eastregion
  3. group ← region

    2region <- c("north", "south", "north", "east")3group <- c("region", "single", "pair")[group_choice]4facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))
    values this stepregiongroup1group_choice
  4. facet ← north, south, north, east

    3group <- c("region", "single", "pair")[group_choice]4facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))5panels <- length(unique(facet))
    values this stepnorth, south, north, eastfacetregiongroup
  5. panels ← 3

    4facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))5panels <- length(unique(facet))6label <- paste(group, panels, sep = ":")
    values this step3panelsnorth, south, north, eastfacet
  6. label ← region:3

    5panels <- length(unique(facet))6label <- paste(group, panels, sep = ":")7cat(label, "\n", sep = "")
    values this stepregion:3labelregiongroup3panels
  7. cat(label, " ", sep = "")

    6label <- paste(group, panels, sep = ":")7cat(label, "\n", sep = "")
    outputregion:3
    values this stepregion:3label
  1. group_choice ← 3

    1group_choice <- 32region <- c("north", "south", "north", "east")
    values this step3group_choice
  2. region ← north, south, north, east

    1group_choice <- 32region <- c("north", "south", "north", "east")3group <- c("region", "single", "pair")[group_choice]
    values this stepnorth, south, north, eastregion
  3. group ← pair

    2region <- c("north", "south", "north", "east")3group <- c("region", "single", "pair")[group_choice]4facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))
    values this steppairgroup3group_choice
  4. facet ← A, A, B, B

    3group <- c("region", "single", "pair")[group_choice]4facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))5panels <- length(unique(facet))
    values this stepA, A, B, Bfacetpairgroup
  5. panels ← 2

    4facet <- switch(group, region = region, single = rep("all", length(region)), pair = c("A", "A", "B", "B"))5panels <- length(unique(facet))6label <- paste(group, panels, sep = ":")
    values this step2panelsA, A, B, Bfacet
  6. label ← pair:2

    5panels <- length(unique(facet))6label <- paste(group, panels, sep = ":")7cat(label, "\n", sep = "")
    values this steppair:2labelpairgroup2panels
  7. cat(label, " ", sep = "")

    6label <- paste(group, panels, sep = ":")7cat(label, "\n", sep = "")
    outputpair:2
    values this steppair:2label
facet A facet key assigns each row to a panel.
unique panels `length(unique(facet))` counts how many panels are needed.
group strategy Changing the grouping strategy changes the chart layout without changing the data rows.