GitHub-style waffle plots in R

R visualization data science tutorial

Attractive visualization for plotting activity over time in R with ggplot2.

Matti Vuorre https://vuorre.netlify.com (University of Oxford)https://www.oii.ox.ac.uk/people/matti-vuorre/
2016-03-24

In this post, I’ll show how to create GitHub style “waffle” plot in R with the ggplot2 plotting package. We’ll use these packages

Simulate activity data

First, I’ll create a data frame for the simulated data, initializing the data types:

d <- tibble(
  date = as.Date(1:813, origin = "2014-01-01"),
  year = format(date, "%Y"),
  week = as.integer(format(date, "%W")) + 1, # Week starts at 1
  day = factor(weekdays(date, T),
    levels = rev(c(
      "Mon", "Tue", "Wed", "Thu",
      "Fri", "Sat", "Sun"
    ))
  ),
  hours = 0
)

And then simulate hours worked for each date. I’ll simulate hours worked separately for weekends and weekdays to make the resulting data a little more realistic, and also simulate missing values to data (that is, days when no work occurred).

set.seed(1)
# Simulate weekends
weekends <- filter(d, grepl("S(at|un)", day))
# Hours worked are (might be) poisson distributed
weekends$hours <- rpois(nrow(weekends), lambda = 4)
# Simulate missing days with probability .7
weekends$na <- rbinom(nrow(weekends), 1, 0.7)
weekends$hours <- ifelse(weekends$na, NA, weekends$hours)

# Simulate weekdays
weekdays <- filter(d, !grepl("S(at|un)", day))
weekdays$hours <- rpois(nrow(weekdays), lambda = 8) # Greater lambda
weekdays$na <- rbinom(nrow(weekdays), 1, 0.1) # Smaller p(missing)
weekdays$hours <- ifelse(weekdays$na, NA, weekdays$hours)

# Concatenate weekends and weekdays and arrange by date
d <- bind_rows(weekends, weekdays) %>%
  arrange(date) %>% # Arrange by date
  select(-na) # Remove na column

Waffle-plot function

Then I’ll create a function that draws the waffle plot. If you have similarly structured data, you can copy-paste the function and use it on your data.

gh_waffle <- function(data, pal = "D", dir = -1) {
  p <- ggplot(data, aes(x = week, y = day, fill = hours)) +
    scale_fill_viridis_c(
      name = "Hours",
      option = pal, # Variable color palette
      direction = dir, # Variable color direction
      na.value = "grey90",
      limits = c(0, max(data$hours))
    ) +
    geom_tile(color = "white", size = 0.7) +
    facet_wrap("year", ncol = 1) +
    scale_x_continuous(
      expand = c(0, 0),
      breaks = seq(1, 52, length = 12),
      labels = c(
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
      )
    ) +
    theme_linedraw(base_family = "Helvetica") +
    theme(
      axis.title = element_blank(),
      axis.ticks = element_blank(), 
      axis.text.y = element_text(size = 7),
      panel.grid = element_blank(),
      legend.position = "bottom",
      aspect.ratio = 1/7,
      legend.key.width = unit(1, "cm"),
      strip.text = element_text(hjust = 0.00, face = "bold", size = 12)
    )

  print(p)
}

Using the waffle plot function

gh_waffle() takes three arguments, the first, data is a data frame with columns date (type: Date), year (number or character), week (number), day (an ordered factor to make days run from top to bottom on the graph), and hours (number). The second option to gh_waffle(), pal specifies one of four color palettes used by the viridis color scale, and can be "A", "B", "C", or "D". The default is “D,” which is also what GitHub uses (or something similar at least). The last option, dir specifies the direction of the color scale, and can be either -1 or 1. The GitHub default is -1.

Using gh_waffle() with the default settings, only providing the data frame d, gives the following result:

gh_waffle(d)

Further reading

Support this work

Software used

The following software packages were used in this blog post: R [Version 4.0.3; R Core Team (2020)] and the R-packages dplyr [Version 1.0.5; Wickham et al. (2021)], forcats [Version 0.5.1; Wickham (2021a)], ggplot2 [Version 3.3.3; Wickham (2016)], knitr [Version 1.31; Xie (2015)], purrr [Version 0.3.4; Henry and Wickham (2020)], readr [Version 1.4.0; Wickham and Hester (2020)], stringr [Version 1.4.0; Wickham (2019)], tibble [Version 3.1.0; Müller and Wickham (2021)], tidyr [Version 1.1.3; Wickham (2021b)], and tidyverse [Version 1.3.0; Wickham et al. (2019)].

Henry, Lionel, and Hadley Wickham. 2020. Purrr: Functional Programming Tools. https://CRAN.R-project.org/package=purrr.
Müller, Kirill, and Hadley Wickham. 2021. Tibble: Simple Data Frames. https://CRAN.R-project.org/package=tibble.
R Core Team. 2020. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2019. Stringr: Simple, Consistent Wrappers for Common String Operations. https://CRAN.R-project.org/package=stringr.
———. 2021a. Forcats: Tools for Working with Categorical Variables (Factors). https://CRAN.R-project.org/package=forcats.
———. 2021b. Tidyr: Tidy Messy Data. https://CRAN.R-project.org/package=tidyr.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2021. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.
Wickham, Hadley, and Jim Hester. 2020. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.
Xie, Yihui. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. https://yihui.org/knitr/.

References

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/mvuorre/mvuorre.github.io, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Vuorre (2016, March 24). Sometimes I R: GitHub-style waffle plots in R. Retrieved from https://mvuorre.github.io/posts/2016-03-24-github-waffle-plot/

BibTeX citation

@misc{vuorre2016github-style,
  author = {Vuorre, Matti},
  title = {Sometimes I R: GitHub-style waffle plots in R},
  url = {https://mvuorre.github.io/posts/2016-03-24-github-waffle-plot/},
  year = {2016}
}