class: center, middle, inverse, title-slide # Lec 06: Color Theory ## SDS 192: Introduction to Data Science ###
Shiya Cao
Statistical & Data Sciences
, Smith College
###
Fall 2024
--- # Today's Learning Goals * Add colors effectively using `ggplot2`. --- # `color` vs `fill` * When mapping a variable to `color` or `fill`, you must put `color = VARIABLE` or `fill = VARIABLE` within the `aes()` argument * `color` gets used for points and lines * `fill` gets used for polygons * After mapping a variable to `color` or `fill`, if you want to change the default colors, you can add color palettes or manually change colors --- # `brewer` vs `distiller` * Both `scale_*_brewer` and `scale_*_distiller` get used to add color palettes * `scale_*_brewer(palette = "PALETTE NAME")` * `scale_*_distiller(palette = "PALETTE NAME")` * `scale_*_brewer` gets used for categorical variables * `scale_*_distiller` gets used for numerical variables --- #
colorbrewer2.org
* `RColorBrewer` package * Sequential color palette: A uni-directional ordering of shades for numerical variables * Diverging color palette: A bi-directional ordering of shades for numerical variables * Qualitative color palette: A discrete set of colors for categorical variables * Also see
Figure 2.11 in MDSR
--- # `scale_*_manual` * `scale_color_manual` or `scale_fill_manual` gets used to manually change colors * `scale_color_manual(values = c("COLOR NAME/CODE", "COLOR NAME/CODE"))` * `scale_fill_manual(values = c("COLOR NAME/CODE", "COLOR NAME/CODE"))` * Run `colors()` in your console to get English names of all colors in R * Also define colors in terms of
hex codes
--- #
`scale_*_viridis`
* More color palettes: `viridis` package * `scale_*_viridis`: default viridis color scales * `scale_*_viridis(option = "COLOR SCALES NAME")` * `scale_*_viridis` by default gets used for numerical variables * `scale_*_viridis(discrete = TRUE)` can be used for categorical variables