R bloggers

Simplifying ggplot2 code by doing nothing

FavoriteLoadingAdd to favorites

Recently, I joined the development team forbayesplot, an R package by the Stan team forplotting Bayesian models. Because visualizing Bayesian models in ggplot2 is arecurringtopichere, it was a natural fit. So from time totime, I’ll post about some programming techniques and new features we develop inthe bayesplot package. For this post, I describe one of strategies I have beenusing to clean up and simplify some of the plotting code in the package: avoidif-branches by sometimes plotting nothing. Warm-up example Let’s start with a non-plotting example. We consider a functionthat takes a list of counts of people and returns the total number of people.That sum, however, is controlled by some of the function’s arguments: head_count <- function(data, staff = FALSE, faculty = FALSE) { total <- data[[“students”]] if (staff) { total <- total + data[[“staff”]] } if (faculty) { total <-…
Original Post: Simplifying ggplot2 code by doing nothing