CommencerCommencer gratuitement

Wrapping for many levels

facet_grid() is fantastic for categorical variables with a small number of levels. Although it is possible to facet variables with many levels, the resulting plot will be very wide or very tall, which can make it difficult to view.

The solution is to use facet_wrap() which separates levels along one axis but wraps all the subsets across a given number of rows or columns.

For this plot, we'll use the Vocab dataset that we've already seen. The base layer is provided.

Since we have many years, it doesn't make sense to use facet_grid(), so let's try facet_wrap() instead.

Cet exercice fait partie du cours

Intermediate Data Visualization with ggplot2

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

ggplot(Vocab, aes(x = education, y = vocabulary)) +
  stat_smooth(method = "lm", se = FALSE) +
  # Create facets, wrapping by year, using vars()
  ___(___(___))
Modifier et exécuter le code