Search code examples
rggplot2facet-wrapfacet-grid

Looking to solve problem with facet_wrap/facet_grid that isn't resolved with space = "free"


I am making a horizontal bar graph with multiple different facets (y axis is categorical variable, x-axis is percentages). I am running into the problem that if I use facet_wrap() the bar widths are different in each of the facets (with weird formatting if I add space="free"). I learned this problem can be solved with facet_grid() but then I have the problem of not having numbered x-axises under each facet.

See below:

library(tidyverse)

df <- structure(list(Race = c("Asian", "Black"), 
    Symptom1 = c(60L, 40L),
    Symptom2 = c(50L, 20L),
    Symptom3 = c(20L, 50L),
    Symptom4 = c(70L, 50L),
    Symptom5 = c(90L, 85L),
    Symptom6 = c(100L, 30L),
    Symptom7 = c(10L, 30L),
    Symptom8 = c(30L, 20L),
    Symptom9 = c(40L, 80L),
    Symptom10 = c(60L, 40L)), 
    row.names = c(NA, -2L), class = "data.frame")

 b <- df %>% pivot_longer(!Race, names_to = "Symptom", values_to ="Percentage")
 b$Symptom <- fct_rev(factor(b$Symptom, levels = unique(b$Symptom)))

b$Group <- "Benign"
b[c(3:5,13:15),]$Group <- "Warning"
b[c(6:10,16:20),]$Group <- "Fatal"

This provides a graph that has same bar widths but only displays the x axis at the very bottom (as opposed to also under the benign and fatal facets as well)

ggplot(data=b, aes(x=Percentage, y= Symptom, fill = Race)) +
    geom_bar(stat="identity", position=position_dodge()) +
    facet_grid(Group~., scales = "free", space = "free")

This provides a graph with a numbered x-axis under each facet but uneven bar widths

ggplot(data=b, aes(x=Percentage, y= Symptom, fill = Race)) +
    geom_bar(stat="identity", position=position_dodge()) +
    facet_wrap(~Group, scales = "free")

This provides even bar widths and a numbered x-axis but also then creates empty "space" in each facet.

ggplot(data=b, aes(x=Percentage, y= Symptom, fill = Race)) +
    geom_bar(stat="identity", position=position_dodge()) +
    facet_wrap(~Group)

Is there a solution to this? I would love a graph that has facets, has equal bar widths in each facet, and has repeated numbered x-axises under each of the facets.

Thanks!


Solution

  • You can get this easily with lemon::facet_rep_grid(repeat.tick.labels = T) instead of ggplot2::facet_grid().

    library(tidyverse)
    library(lemon)
    
    b <- structure(list(Race = c("Asian", "Asian", "Asian", "Asian", "Asian", "Asian", "Asian", "Asian", "Asian", "Asian", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black"), Symptom = structure(c(10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 10L, 9L,  8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), .Label = c("Symptom10", "Symptom9", "Symptom8", "Symptom7", "Symptom6", "Symptom5", "Symptom4", "Symptom3", "Symptom2", "Symptom1"), class = "factor"), Percentage = c(60L, 50L, 20L, 70L, 90L, 100L, 10L, 30L, 40L, 60L, 40L, 20L, 50L, 50L, 85L, 30L, 30L, 20L, 80L, 40L), Group = c("Benign", "Benign", "Warning", "Warning", "Warning", "Fatal", "Fatal",  "Fatal", "Fatal", "Fatal", "Benign", "Benign", "Warning", "Warning", "Warning", "Fatal", "Fatal", "Fatal", "Fatal", "Fatal")), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))
    
    ggplot(data=b, aes(x=Percentage, y= Symptom, fill = Race)) +
      geom_bar(stat="identity", position=position_dodge()) +
      facet_rep_grid(Group~., scales = "free", space = "free", repeat.tick.labels = T)
    

    Created on 2022-01-18 by the reprex package (v2.0.1)