1

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"))
Liza
  • 1,066
  • 2
  • 16
  • 26
  • 1
    You could try `remotes::install_github("liamgilbey/ggwaffle")` That package has a working `geom_waffle`. Otherwise, please provide a reproducible by providing the output of `dput(storms)`. – Ian Campbell Jan 15 '21 at 02:32
  • from which package you have used `extract2`? – AnilGoyal Jan 15 '21 at 05:55
  • @IanCampbell, `storms` is a sample data available in `dplyr` – AnilGoyal Jan 15 '21 at 05:57
  • Thank you @IanCampbell, for some reason when I installed the waffle package the first time it didn't have all the functions working, even though it didn't show any warnings or errors during installation. – Liza Jan 15 '21 at 17:06

1 Answers1

1

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.

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
  • Thank you @AnilGoyal. I had to reinstall my waffle package, now it's working. For some reason when I installed the waffle package the first time it didn't have all the functions working, even though it didn't show any warnings or errors during installation. – Liza Jan 15 '21 at 17:07
  • Seems like a different version is available on cran-r – AnilGoyal Jan 16 '21 at 03:03