I am creating a figure using ggplot2 and I need the axis labels to be different colors. Is this possible to do with ggplot2 only (no other pkgs) AND is officially supported by ggplot2. (This is code that will be added to a package, and I can't add another dependency, and nervous to implement something that is not officially supported in case it breaks!)
This post (customize ggplot2 axis labels with different colors) has a couple of solutions for this problem. But each of them have an issue for my use case: 1. Using vectorized colors results in a scary warning from ggplot2 about this not being officially supported (method implemented below), or 2. They require the ggtext package.
Thank you!
library(ggplot2)
mtcars |>
dplyr::mutate(
color =
c("BLUE", "RED") |> rep_len(dplyr::n()) |>
factor()
) |>
ggplot(aes(x = mpg, y = color)) +
geom_point() +
theme(axis.text.y.left = element_text(color = c("blue", "red"))) +
labs(y = "")
#> Warning: Vectorized input to `element_text()` is not officially supported.
#> Results may be unexpected or may change in future versions of ggplot2.
Created on 2022-08-09 by the reprex package (v2.0.1)