Search code examples
rgraphwaffle-chart

Waffle chart with 2 categories and raw counts fills out final column with a mysterious 3rd category of made up data


I'm trying to make a waffle chart of raw counts and only two categories. For some reason it makes up enough counts of a mysterious 3rd category to fill out the final column. With three or more categories the final column is partially filled as I intend. How do I make this work with just two categories?

My code:

library(waffle)

test2 <- c(`Group A`= 40, `Group B`= 33)
test3 <- c(`Group A`= 40, `Group B`= 33, `Group c` =10)

#test2 fails, test3 works as intended
waffle(test2)  
waffle(test3)

images below


Solution

  • As far as I get it the issue is related to the fact that the default color brewer palette returns three colors, i.e RColorBrewer::brewer.pal(2, "Set2") will return a vector of three colors. To solve this issue you have to explicitly specify that you want to have only two colors in your waffle chart. Try this:

    library(waffle)
    #> Loading required package: ggplot2
    
    test2 <- c(`Group A`= 40, `Group B`= 33)
    
    waffle(test2, colors = c(RColorBrewer::brewer.pal(3, "Set2")[1:2]))