Colors and text in R
Colours
See Color Pitfalls on this webpage.
The colorspace package can be used to A LOT of things. Examples:
Text
See package ggtext for ways to fx. color text labels, titles, etc. based on categories in data.
- Just specify the text using HTML code and specify the relevant argument in
ggplot2::theme()using the functionggtext::element_markdown().- See an example of how to color your title in question 3 of lab5 at UoA.
See also gridtext for text rendering support for grid graphics in R.
Fonts
See Fonts in R for a small introduction
Getting new fonts
Download font online:
- Go to font download site, fx. Google Gruppo
- Click download
- When downloaded, extract it 3a) On windows, click the
Gruppo-regularfile and press “Install” - In R, use the cairo graphics device 4a) In Rstudio: Tools -> Global options -> General -> Graphics (top) -> Backend -> Choose cairo 4b) In Rmarkdown: Include
knitr::opts_chunk$set(dev.args = list(png = list(type ="cairo")))in setup chunk. - Refer to the new font by it’s folder name in your system. Fx.
ggplot2::element_text(family = "Gruppo")
References
- “Selecting colours in R”
- Various R functions to generate colors
- The Colorspace R package
- “A Toolbox for Manipulating and Assessing Colors and Palettes”
- “Coloring in R’s Blind Spot” by Achim Zeileis and Paul Murrell
- A walkthrough of new R color and palette functions introduced with R version 4.0.0
Practical advice on how to use colors most “efficiently” in your plots