rggplot2wafflewaffle-chart

Waffle Bar Chart with scale


I'm trying to recreate this waffle bar chart: https://github.com/hrbrmstr/waffle#waffle-bar-charts-with-scales

library(dplyr)
library(waffle)

storms %>% 
  filter(year >= 2010) %>% 
  count(year, status) -> storms_df

ggplot(storms_df, aes(fill = status, values = n)) +
  geom_waffle(color = "white", size = .25, n_rows = 10, flip = TRUE) +
  facet_wrap(~year, nrow = 1, strip.position = "bottom") +
  scale_x_discrete() + 
  scale_y_continuous(labels = function(x) x * 10, # make this multiplyer the same as n_rows
                     expand = c(0,0)) +
  ggthemes::scale_fill_tableau(name=NULL) +
  coord_equal() +
  labs(
    title = "Faceted Waffle Bar Chart",
    subtitle = "{dplyr} storms data",
    x = "Year",
    y = "Count"
  ) +
  theme_minimal(base_family = "Roboto Condensed") +
  theme(panel.grid = element_blank(), axis.ticks.y = element_line()) +
  guides(fill = guide_legend(reverse = TRUE))

And it seems that geom_waffle is no longer available, it's waffle now and they changed some arguments as well. So I created a named vector and fixed the color argument, but it's still not working:

storms %>% 
  filter(year >= 2010) %>% 
  count(status) -> storms_df2
vec = extract2(storms_df2, 'n') %>% set_names(storms_df2$status)

ggplot(storms_df, aes(fill = status, values = n)) +
  waffle(vec,colors=c("red","green","blue"), size = .25, rows = 10, flip = TRUE) +
  facet_wrap(~year, nrow = 1, strip.position = "bottom") +
  scale_x_discrete() + 
  scale_y_continuous(labels = function(x) x * 10, # make this multiplyer the same as n_rows
                     expand = c(0,0)) +
  ggthemes::scale_fill_tableau(name=NULL) +
  coord_equal() +
  labs(
    title = "Faceted Waffle Bar Chart",
    subtitle = "{dplyr} storms data",
    x = "Year",
    y = "Count"
  ) +
  theme_minimal(base_family = "Roboto Condensed") +
  theme(panel.grid = element_blank(), axis.ticks.y = element_line()) +
  guides(fill = guide_legend(reverse = TRUE))

What am I missing? The waffle funstion on it's own is working, but I need a bar chart by year:

waffle(vec, rows = 50, colors=c("red","green","blue"))

Solution

  • If you'll install waffle from this repository you'll be able to create this chart

    install.packages("waffle", repos = "https://cinc.rud.is")
    
    
    library(tidyverse)
    library(waffle)
    library(ggthemes)
    
    storms %>% 
      filter(year >= 2010) %>% 
      count(year, status) -> storms_df
    
    ggplot(storms_df, aes(fill = status, values = n)) +
      geom_waffle(color = "white", size = .25, rows = 10, flip = TRUE) +
      facet_wrap(~year, nrow = 1, strip.position = "bottom") +
      scale_x_discrete() + 
      scale_y_continuous(labels = function(x) x * 10, # make this multiplyer the same as n_rows
                         expand = c(0,0)) +
      ggthemes::scale_fill_tableau(name=NULL) +
      coord_equal() +
      labs(
        title = "Faceted Waffle Bar Chart",
        subtitle = "Created by Anil Goyal",
        x = "Year",
        y = "Count"
      ) +
      theme_minimal(base_family = "Roboto Condensed") +
      theme(panel.grid = element_blank(), axis.ticks.y = element_line()) +
      guides(fill = guide_legend(reverse = TRUE))
    
    

    enter image description here

    I have changed the subtitle just to show that it is still working.