1

I am having trouble making the subtitle for my ggplot2 graph. I tried the answer here but it is only applicable if you have one scientific name. In my case, I have two scientific names that I wanted to include in the plot title.

This is the subtitle that I want to include:

"High quantity of Bullet tuna (Auxis rochei) and Buccaneer anchovy (Encrasicholina punctifer) were attributed to the increase in the landings in 2016."

I want it to be multi-line because it is very long.

My initial code (one line):

mysubtitle <- expression(paste("High quantity of Bullet tuna ", italic("Auxis rochei"), " and ", "Buccaneer anchovy ", italic("Encrasicholina punctifer"), " were attributed to the increase in the landings in 2016."))

I tried:

mysubtitle <- expression(atop(paste("High quantity of Bullet tuna (", italic("Auxis rochei"), ") and ", "Buccaneer anchovy (", italic("Encrasicholina punctifer"), ")"), paste("were attributed to the increase in the landings in 2016.")))

The above code generated a two line title, BUT it is centered although in my ggplot2 theme plot.subtitle = element_text(hjust = 0). I want it to be left aligned the same as of the main plot title.

Note: I do not have trouble in the main plot title (termed as title in ggplot2 labs function). Only in the subtitle. In addition, I have a separate main title.

turonlapis
  • 13
  • 5

1 Answers1

0

From the "it's stupid but it works", you can add holder strings to the right of the second line to force left alignment. The right holder strings could be arbitrary character whose length is nchar(first_line - nchar(second_line))

library(ggplot2)
first_line<- "High quantity of Bullet tuna (Auxis rochei) and Buccaneer anchovy (Encrasicholina punctifer)"
second_line <- "were attributed to the increase in the landings in 2016."
# the length of holder_string
holder_length <- nchar(first_line) - nchar(second_line)
# generate a arbitrary string with length `holder_length`
holder_string <- stringi::stri_rand_strings(1, holder_length)
# the default font family of ggplot is `sans`, we should set the font family as `mono` (monospaced font) to ensure strings between two lines aligned. 
p  <- ggplot(mtcars, aes(wt, mpg))+ geom_point()
p + labs(subtitle = bquote(atop(
  paste("High quantity of Bullet tuna (", 
    italic("Auxis rochei"), ") and ", "Buccaneer anchovy (", 
    italic("Encrasicholina punctifer"), ")"), 
  paste("were attributed to the increase in the landings in 2016.", 
    phantom(.(holder_string))
  )))
) +
theme(plot.subtitle = element_text(family = "mono"))

EDITING

Or, you can use substitute()

p + labs(subtitle = substitute(atop(
  paste("High quantity of Bullet tuna (", 
    italic("Auxis rochei"), ") and ", "Buccaneer anchovy (", 
    italic("Encrasicholina punctifer"), ")"), 
  paste("were attributed to the increase in the landings in 2016.", 
    phantom(A))
  ),list(A = holder_string))) + 
theme(plot.subtitle = element_text(family = "mono"))

Hoping this helps!

yang
  • 719
  • 3
  • 11
  • This helps. Can you suggest references about your answer so that I can read it to further enhance my learnings? – turonlapis Jan 30 '18 at 01:42
  • We can set the font family to `mono` to ensure letters and spacings have the same widths. Then arbitrary characters in length `nchar(first_line) - nchar(second_line)` could be generated as holder character. – yang Jan 30 '18 at 12:36
  • The `phantom()` is a feature in plotmath, and can be used to create invisible character. [This post](https://stackoverflow.com/questions/4423130/use-a-variable-within-a-plotmath-expression?answertab=active#tab-top) shows how to use a variable within a plotmath expression (just like how to use `holder_string` in the above answer. – yang Jan 30 '18 at 12:58