Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

r - Multi line title in ggplot 2 with multiple italicized words

I am trying to create a plot title manually formatted on two lines which includes two italicized words, I have done some searching on Stack Exchange but have not found a good solution to this seemingly simple problem.

The scientific names of the two species are fairly long, and thus the need for a multi-line title (ggplot2 doesn't format this).

Objective:

..........First Line of Title with Species

Second line words anotherItalicSpecies the end

ggplot(mtcars,aes(x=wt,y=mpg))+
  geom_point()+
  labs(title= expression(paste(atop("First line of title with ", atop((italic("Species")))),"
       secondline words", italic("anotherSpecies"), "the end")))

Which yields the following mangled title:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using a combination of atop, paste, italic and scriptstyle:

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = ~ atop(paste('First line of title with ',italic("Species")),
                      paste(scriptstyle(italic("Species")),
                            scriptstyle(" secondline words "),
                            scriptstyle(italic("anotherSpecies")),
                            scriptstyle(" the end"))))

gives you the desired result:

enter image description here

Using scriptstyle is not a necessity, but imho it is nicer to have your subtitle in a smaller font than the main title.

See also ?plotmath for other usefull customizations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...