简体   繁体   中英

Multi line plot subtitle with scientific names in ggplot2

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM