I recently saw a line chart in the Economist where the title had colored words to match the colors of the groups used in the line chart. I was wondering how to do this with
Here's a simple and more general way using the ggtext package
produced with:
library(ggtext)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(size = 3) +
scale_color_manual(
name = NULL,
values = c(setosa = "#0072B2", virginica = "#009E73", versicolor = "#D55E00"),
labels = c(
setosa = "I. setosa",
virginica = "I. virginica",
versicolor = "I. versicolor")
) +
labs(
title = "**Fisher's *Iris* dataset**
Sepal width vs. sepal length for
setosa,
versicolor, and
virginica
",
x = "Sepal length (cm)", y = "Sepal width (cm)"
) +
theme_minimal() +
theme(
plot.title = element_markdown(lineheight = 1.1),
legend.text = element_markdown(size = 11)
)