ggplot2 Patterns

Here are some useful ggplot2 patterns.

Different Datasets per geom

We'll use the dataframe mpg, simply because it's already available in ggplot2.

library(ggplot2)
mpg <- ggplot2::mpg

Let's create a second dataframe that jives with mpg.

x <- mpg$cyl
y <- 3 * x + rexp(length(x))
df <- data.frame(x = x, y = y)

The most common pattern, in my experience, with ggplot2 is to specify one dataframe inside the call to ggplot(). This dataframe is then re-used across all following geom_()s.

ggplot(mpg) + # use mpg for all following geoms
    geom_jitter(aes(cyl, cty), color = "orange") +
    ...

You can also specify within each geom_() a different dataframe, so as to layer information from different data sources.

ggplot() + # specify a new dataset per geom
  geom_jitter(data = mpg, aes(cyl, cty), color = "orange") +
  geom_jitter(data = df, aes(x, y), color = "blue")

You can also combine these to say, use this dataframe for all following geom_()s up until the dataframe is changed.

ggplot(mpg) + # use mpg for all following geoms, until I say otherwise
  geom_jitter(aes(cyl, cty), color = "orange") +
  geom_jitter(data = df, aes(x, y), color = "blue")