16.7 Grouping vs. faceting

With faceting, each group is quite far apart in its own panel, and there is no overlap between the groups. This is good if the groups overlap a lot, but it does make small differences harder to see.

When using aesthetics to differentiate groups, the groups are close together and may overlap, but small differences are easier to see.

df <- data.frame(
  x = rnorm(120, c(0, 2, 4)),
  y = rnorm(120, c(1, 2, 1)),
  z = letters[1:3]
)

ggplot(df, aes(x, y)) + 
  geom_point(aes(colour = z))

ggplot(df, aes(x, y)) + 
  geom_point(aes(color=z)) + 
  facet_wrap(~z)

You can help the viewer make comparisons across facets with some thoughtful annotation.

In this example, we show the mean of every group in each panel.

df_sum <- df %>% 
  group_by(z) %>% 
  summarise(x = mean(x), y = mean(y)) %>%
  rename(z2 = z)


ggplot(df, aes(x, y)) + 
  geom_point() + 
  geom_point(data = df_sum, aes(colour = z2), size = 4) + 
  facet_wrap(~z)

Alternatively, we could put all data points in each panel, and only colour the focal data.

df2 <- dplyr::select(df, -z)

ggplot(df, aes(x, y)) + 
  geom_point(data = df2, colour = "grey70") +
  geom_point(aes(colour = z)) + 
  facet_wrap(~z)