1

I'm creating a dynamically generated title for multiple plots. For example, below are 3 plots titles I'm trying to make.

title_list = c("awesome", "amazing", "fantastic")

title suffix = "title"

Intended plot titles: awesome title, amazing title , fantastic title.

Notice "title" is not italicized, but "awesome", "amazing" and "fantastic" are. How can I create titles like this?

Gregor Thomas
  • 119,032
  • 17
  • 152
  • 277
Alexander
  • 798
  • 1
  • 9
  • 14

2 Answers2

1

In case anyone was curious, I figured it out.

plot_title <- substitute(paste(italic(x), "title", sep=" "), list(x=title_list))

p + labs(title=plot_title)
Alexander
  • 798
  • 1
  • 9
  • 14
1

You can use bquote for this. Inside bquote, expressions wrapped in .() will be evaluated.

p = list()
for (i in seq_along(title_list)) {
    p[[i]] = ggplot(mtcars, aes(wt, mpg)) +
        geom_point() +
        labs(title = bquote(italic(.(title_list[i])) ~ .(title_suffix)))
}

gridExtra::grid.arrange(p[[1]], p[[2]], p[[3]])

enter image description here

A very related question is this one.

Gregor Thomas
  • 119,032
  • 17
  • 152
  • 277