Facet labels and order

Visualizzazione dei dati intermedia con ggplot2

Rick Scavetta

Founder, Scavetta Academy

A new dataframe

# Plot
p <- ggplot(msleep2, aes(bodywt_log,
                         brainwt_log)) +
  geom_point(alpha = 0.6, shape = 16) +
  coord_fixed()

p

Visualizzazione dei dati intermedia con ggplot2

A new dataframe, with facets

p +
  facet_grid(rows = vars(vore))

Visualizzazione dei dati intermedia con ggplot2

A new dataframe, with facets

p +
  facet_grid(rows = vars(vore))

Visualizzazione dei dati intermedia con ggplot2

Poor labels and order

p +
  facet_grid(rows = vars(vore))

Two typical problems with facets:

  • Poorly labeled (e.g. non descriptive)
  • Wrong or inappropriate order

Visualizzazione dei dati intermedia con ggplot2

Poor labels and order

p +
  facet_grid(rows = vars(vore))

Solutions:

  • Easy: Add labels in ggplot
  • Better: Relabel and rearrange factor variables in your dataframe

Visualizzazione dei dati intermedia con ggplot2

The labeller argument

# Default is to label the value
p +
  facet_grid(rows = vars(vore), 
             labeller = label_value)

Visualizzazione dei dati intermedia con ggplot2

Using label_both adds the variable name

# Print variable name also
p +
  facet_grid(rows = vars(vore), 
               labeller = label_both)

Visualizzazione dei dati intermedia con ggplot2

Two variables on one side

p +
  facet_grid(rows = vars(vore, 
                         conservation))

Visualizzazione dei dati intermedia con ggplot2

Using label_context avoids ambiguity

p +
  facet_grid(rows = vars(vore,
                         conservation),
               labeller = label_context)

Visualizzazione dei dati intermedia con ggplot2

Use rows and columns when appropriate

p +
  facet_grid(rows = vars(vore), 
             cols = vars(conservation),
             labeller = label_context)

Visualizzazione dei dati intermedia con ggplot2

Use rows and columns when appropriate

p +
  facet_grid(rows = vars(vore), 
             cols = vars(conservation))

Visualizzazione dei dati intermedia con ggplot2

Use rows and columns when appropriate

Visualizzazione dei dati intermedia con ggplot2

Relabeling and reordering factors

msleep2$conservation <- fct_recode(msleep2$conservation,
                                   Domesticated = "domesticated",
                                   `Least concern` = "lc",
                                   `Near threatened` = "nt",
                                   Vulnerable = "vu",
                                   Endangered = "en")

msleep2$vore = fct_recode(msleep2$vore,
                          Carnivore = "carni",
                          Herbivore = "herbi",
                          Insectivore = "insecti",
                          Omnivore = "omni")
Visualizzazione dei dati intermedia con ggplot2

Reinitialize plot with new labels

# Plot
p <- ggplot(msleep2, aes(bodywt_log,
                         brainwt_log)) +
  geom_point(alpha = 0.6, shape = 16) +
  coord_fixed()

p +
  facet_grid(rows = vars(vore), 
             cols = vars(conservation)) 

Visualizzazione dei dati intermedia con ggplot2

Reinitialize plot with new labels

Visualizzazione dei dati intermedia con ggplot2

Changing the order of levels

# Change order of levels:
msleep2$conservation = fct_relevel(msleep2$conservation,
                                   c("Domesticated",
                                     "Least concern",
                                     "Near threatened",
                                     "Vulnerable",
                                     "Endangered"))

Visualizzazione dei dati intermedia con ggplot2

Reinitialize plot with new order

Visualizzazione dei dati intermedia con ggplot2

Let's practice!

Visualizzazione dei dati intermedia con ggplot2

Preparing Video For Download...