Primary exercises

  1. Facets (grid): variable for rows and/or variable for columns.
    Try each of the following facet_grid lines and understand the effects:
p <- ggplot( pulse ) +
  aes( x = weight, y = height, color = exercise, shape = gender ) +
  geom_point( size = 3, alpha = 0.8 )
p + facet_grid( . ~ exercise )
p + facet_grid( exercise ~ . )
p + facet_grid( gender ~ exercise )
p + facet_grid( exercise ~ gender )
p + facet_grid( exercise ~ gender, scales = "free" )
  1. Facets (wrap): multi-level variable in multiple rows and columns.
    Now compare facet_wrap with facet_grid:
p <- ggplot( pulse ) +
  aes( x = weight, y = height, color = exercise, shape = gender ) +
  geom_point( size = 3, alpha = 0.8 )
p + facet_grid( . ~ year )
p + facet_grid( year ~ . )
p + facet_wrap( ~ year )
p + facet_wrap( ~ year, scales = "free" )
  1. Visualize effect of running on pulse.
    Reconstruct the following image based on the pulse data.
    Add component coord_fixed() to enforce that unit sizes are identical for both axes.
    Use geom_abline( slope = 1 ) to get the line showing where pulse1 and pulse2 are equal.
    Disable the warning message.

Extra exercises

  1. Rotate axis labels.
    When using facets the numbers on the bottom axis often overlap.
    Modify the chart from the previous exercise by adding (after theme_bw):
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)).
    vjust and hjust are numbers in range [0,1]. Understand their effect.
    Try angle=45.



Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC