I first learned about embedding many small subplots into a larger plot as a way to visualize large datasets with package ggsubplot. Embedding subplots is still possible in ggplot2 today with the annotation_custom() function. I demonstrate one approach to do this, making many subplots in a loop and then adding them to the larger plot.
When working with counts, having many zeros does not necessarily indicate zero inflation. I demonstrate this by simulating data from the negative binomial and generalized Poisson distributions. I then show one way to check if the data has excess zeros compared to the number of zeros expected based on the model.
Analyzing positive data with 0 values can be challenging, since a direct log transformation isn't possible. I discuss some of the things to consider when deciding on an analysis strategy for such data and then explore the effect of the value of the constant, c, when using log(y + c) as the response variable.
In this post I show an example of how to automate the process of making many exploratory plots in ggplot2 with multiple continuous response and explanatory variables. To loop through both x and y variables involves nested looping. In the latter section of the post I go over options for saving the resulting plots, either together in a single document, separately, or by creating combined plots prior to saving.
Checking for autocorrelation must be done carefully when some observations are missing from a time series or the time series is measured for independent groups. I show an approach where I pad the dataset with NA via tidyr::complete() to fill in any missed sampling times and make sure groups are considered independent prior to calculating the autocorrelation function.
I currently work as a consulting statistician, advising natural and social science researchers on statistics, statistical programming, and study design. I create and teach R workshops for applied science graduate students who are just getting started in R, where my goal is to make their transition to a programming language as smooth as possible. See my workshop materials at my website.
An example of base::split() for looping through groups
I recently had a question from a client about the simplest way to subset a data.frame and apply a function to each subset. “Simplest” could mean many things, of course, since what is simple for one person could appear very difficult to another. In this specific case I suggested using base::split() as a possible option since it is one I find fairly approachable. I turns out I don’t have a go-to example for how to get started with a split() approach.
Making a background color gradient in ggplot2
I was recently making some arrangements for the 2020 eclipse in South America, which of course got me thinking of the day we were lucky enough to have a path of totality come to us. We have a weather station that records local temperature every 5 minutes, so after the eclipse I was able to plot the temperature change over the eclipse as we experienced it at our house. Here is an example of a basic plot I made at the time.
Expanding binomial counts to binary 0/1 with purrr::pmap()
Data on successes and failures can be summarized and analyzed as counted proportions via the binomial distribution or as long format 0/1 binary data. I most often see summarized data when there are multiple trials done within a study unit; for example, when tallying up the number of dead trees out of the total number of trees in a plot. If these within-plot trials are all independent, analyzing data in a binary format instead of summarized binomial counts doesn’t change the statistical results.
More exploratory plots with ggplot2 and purrr: Adding conditional elements
This summer I was asked to collaborate on an analysis project with many response variables. As usual, I planned on automating my initial graphical data exploration through the use of functions and purrr::map() as I’ve written about previously. However, this particular project was a follow-up to a previous analysis. In the original analysis, different variables were analyzed on different scales. I wanted to put the new plots on whatever scale they were analyzed in that analysis.
Many similar models - Part 2: Automate model fitting with purrr::map() loops
When we have many similar models to fit, automating at least some portions of the task can be a real time saver. In my last post I demonstrated how to make a function for model fitting. Once you have made such a function it’s possible to loop through variable names and fit a model for each one. In this post I am specifically focusing on having many response variables with the same explanatory variables, using purrr::map() and friends for the looping.
Many similar models - Part 1: How to make a function for model fitting
I worked with several students over the last few months who were fitting many linear models, all with the same basic structure but different response variables. They were struggling to find an efficient way to do this in R while still taking the time to check model assumptions. A first step when working towards a more automated process for fitting many models is to learn how to build model formulas with paste() and as.
The small multiples plot: how to combine ggplot2 plots with one shared axis
There are a variety of ways to combine ggplot2 plots with a single shared axis. However, things can get tricky if you want a lot of control over all plot elements. I demonstrate four different approaches for this: 1. Using facets, which is built in to ggplot2 but doesn’t allow much control over the non-shared axes. 2. Using package cowplot, which has a lot of nice features but the plot spacing doesn’t play well with a single shared axis.
Embedding subplots in ggplot2 graphics
The idea of embedded plots for visualizing a large dataset that has an overplotting problem recently came up in some discussions with students. I first learned about embedded graphics from package ggsubplot. You can still see an old post about that package and about embedded graphics in general, with examples. However, ggsubplot is no longer maintained and doesn’t work with current versions of ggplot2. I poked around a bit, and found that annotation_custom() is the go-to function for embedding plots in a ggplot2 graphic.
Custom contrasts in emmeans
Following up on a previous post, where I demonstrated the basic usage of package emmeans for doing post hoc comparisons, here I’ll demonstrate how to make custom comparisons (aka contrasts). These are comparisons that aren’t encompassed by the built-in functions in the package. Remember that you can explore the available built-in emmeans functions for doing comparisons via ?"contrast-methods". Table of Contents Reasons for custom comparisons R packages The dataset and model Treatment vs control comparisons Building custom contrasts The contrast() function for custom comparisons Using named lists for better output Using “at” for simple comparisons Multiple custom contrasts at once More complicated custom contrasts Just the code, please Reasons for custom comparisons There are a variety of reasons you might need custom comparisons instead of some of the standard, built-in ones.
Getting started with emmeans
Package emmeans (formerly known as lsmeans) is enormously useful for folks wanting to do post hoc comparisons among groups after fitting a model. It has a very thorough set of vignettes (see the vignette topics here), is very flexible with a ton of options, and works out of the box with a lot of different model objects (and can be extended to others 👍). I’ve started recommending emmeans all the time to students fitting models in R.