I sometimes want some words to appear in a specific color on plots made with ggplot2.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.3 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggtext)
2.1 Title and axis
We use the span HTML element to put hexadecimal colors we desire for some text in the arguments x, y or title of the labs() function. Then, we need to update theme() function so that the elements axis.title.x, axis.title.y, and plot.title are correctly interpreted.
ggplot(data = iris,mapping =aes(x = Sepal.Length,y = Sepal.Width )) +geom_point() +labs(x ="<span style='color:#0072B2;'>**Sepal Length**</span>",y ="<span style='font-size:14pt; color:#D55E00;'>Sepal Width</span>",title =str_c("<span style='font-size:14pt; color:#D55E00;'>Sepal Width","</span> as a function of <span style='color:#0072B2;'>","**Length**</span>") ) +theme(plot.title.position ="plot",axis.title.x =element_markdown(),axis.title.y =element_markdown(),plot.title =element_markdown() )
2.2.1 With a different order for the facet elements
We can use a trick, using {forcats} fct_reorder() function:
In a first step, we relevel the orginial variables that is used to create the faceting groups (not the one we just created to make it as a markdown string), using factor(), for example.
In a second step, we use the fct_reorder() on the variable used to create the faceting groups (the one we just created as a markdown string) and we apply the order of the numerical values corresponding to the levels of the releveled variables from Step 1.