XKCD, Randall Munroe, https://xkcd.com/2494/

ggplot2

Customizing scales

Scale functions control the mapping from data to aesthetics and every aesthetic has a default scale.

All scale functions have a common naming scheme: scale_name of aesthetic_name of scale

  • scale_y_continuous
  • scale_color_discrete
  • scale_fill_manual

Scale functions frequenty use functions from the scales package.

Some tidyr

Pivotting

Convert between long and wide data

pivot_longer: Convert wide data to long, or move variable values out of the column names and into the cells.

  • pivot_longer(df, cols = -country, names_to = "year", values_to = "cases")

pivot_wider: Convert long data to wide, or move variable names out of the cells and into the column names.

  • pivot_wider(df, id_cols = country, names_from = type, values_from = count)

See the pivot vignette

Some joins/merging

Joins merge data sets based on key variables. The syntax is always name_join(x, y, by = "key")

Animated visuals created by Garrick Aden-Buie

  • full_join(): keeps all observations in x and y

  • left_join(): keeps all observations in x

  • inner_join(): keeps observations in both x and y