Custom ggplot2 theme that capitalizes title

I am working on creating a custom ggplot2 theme for my work. The style guidelines say that all figure titles should be capitalized. My question is whether there is a way to accomplish this from within the custom theme function. Looking at the options in the element_text function seems that I can't simply do it through that.

I know I could create a custom labs function that would capitalize the title but I would prefer that it is done within the custom theme function so that users don't have to remember to call both the custom theme and the custom labs function.

Here is my desired output:

library(ggplot2)


ggplot(mtcars, aes(mpg, hp)) + 
  geom_point() + 
  labs(
    title = "My plot title"
  ) 



my_labs <- function(title, ...){
  labs(title = stringr::str_to_upper(title), ...)
}
# desried output
ggplot(mtcars, aes(mpg, hp)) + 
  geom_point() + 
  my_labs(
    title = "My plot title"
  ) 

Created on 2019-09-12 by the reprex package (v0.3.0)

The only idea I have so far would be to create a function that returns a list of both the custom labs and the custom theme. I am not entirely sure that is what I want either because I feel like that kind of goes against the feel of ggplot2.

So to be clear, I would prefer to be able to set it via something like:

theme(plot.title = element_text(fontcase = "upper"))

Any suggestions or ideas? Thanks!

1 Like

I found a work around, however it is not 100% reliable as it depends entirely on the order in which the user adds the labels and the custom theme to the ggplot object.

library(ggplot2)

ggplot_add.my_theme <- function(object, plot, object_name){
  plot$theme <- ggplot2:::update_theme(plot$theme, object)
  plot$labels$title <- stringr::str_to_upper(plot$labels$title)
  plot
}

new_theme <- function(){
  out <- theme_bw()
  
  out <- out %+replace% theme(plot.title = element_text(face = "bold"))
  
  class(out) <- c("my_theme", class(out))
  
  return(out)
}


# works
ggplot(mtcars, aes(mpg, hp)) + 
  geom_point() + 
  labs(
    title = "My plot title"
  ) + 
  new_theme()


# doesn't work
ggplot(mtcars, aes(mpg, hp)) + 
  geom_point() + 
  new_theme() + 
  labs(
    title = "My plot title"
  ) 

Created on 2019-09-12 by the reprex package (v0.3.0)

If there are any suggestions on how to make this more stable and less dependent on the order of operations, that would be greatly appreciated

I think this is pretty inherent to the theme-ing system, since it allows you to override and tweak (and ggplot2 in general is additively layered, so the order does make a difference in many cases).

Sorry this doesn't cover the entirety of your question, but I thought chiming in on this one bit would be better than nothing! :slightly_smiling_face:

1 Like

Yea, the more I think about it, the more I like that users can get around it if they want to. I am sure not everyone will love that it does all caps for the title so it will be nice that they can avoid it. I will just be sure to make it clear in the documentation why the order of operations matters.

Thanks!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.