update ggplot labs with a function

When using ggplot, we map columns to aesthetics, so we always end up with axis/legend titles from the column names. However these follow some R symbol casing like snake case or other. However in a plot it's nicer to have sentence- or title case labels.

I was trying to write a function that can take a plot and a renaming function (like stringr::str_to_title() as input and maps the latter over the ggplot labels. A bit like rename_with(), but then labs_with(). So in the end I'm aiming for something that can be used like this:

iris |> 
  ggplot(aes(Sepal.Length, Sepal.Width, fill = Species)) +
  geom_point() +
  labs_with(str_to_title)

This way I don't have to rename everything manually with labs()

However, I got pretty stuck with this. Anybody here any thoughts?

I might be easier to add a label variable in a pipe, use that new variable to map your aesthetics and then use guides to fix the title:

library(ggplot2)
iris |> 
  dplyr::mutate(label = stringr::str_to_title(Species)) |>
  ggplot(
    aes(Sepal.Length, Sepal.Width, color = label)) +
  geom_point()+
  guides(color = guide_legend(title = "Species"))