Home > Mobile >  How to use new line (\n) with italic in ggplot title in R?
How to use new line (\n) with italic in ggplot title in R?

Time:02-03

I want a plot title that uses italics and two lines. I can't figure out how to make such a title.

# Sample data
> dput(samp1)
structure(list(phase = c("Years 2012-2016", "Years 2012-2016", 
"Years 2012-2016", "Years 2012-2016", "Years 2012-2016"), name = c("Camden", 
"Camden", "Camden", "Camden", "Camden"), value = c(37.0665188470067, 
49.4146341463415, 63.1718403547672, 44.6607538802661, 33.2638580931264
), county = structure(c(1L, 1L, 1L, 1L, 1L), levels = c("Camden County", 
"Guilford County", "Pasquotank County", "Wake County"), class = "factor")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L))

I tried

library(tidyverse)

mytesttitle <- expression(atop(paste("Summary of Days with ",italic(Weather),"\nSuited with extra words to make this longer, 2012-2021")))

ggplot(data=samp1)   geom_boxplot(aes(y=value)) 
  labs( y="Number of Days",
  title=mytesttitle)  
  theme(axis.text.x = element_blank(), axis.ticks.x=element_blank())

and there is no new line. The title bleeds off the viewing area. Also the title has a lot of space between the text and the border of the plot itself:

an image of plot with title offscreen

I also tried:

mytesttitle <- expression(paste("Summary of Days with ",italic("Weather"),"\nMore Words Make This Longer Suited, 2012-2021"))

ggplot(data=samp1)   geom_boxplot(aes(y=value)) 
 labs( y="Number of Days",
 title=mytesttitle)   #"Summary of Days with Weather\nSuited, 2012-2021")  
 theme(axis.text.x = element_blank(), axis.ticks.x=element_blank(),
 plot.title = element_text(hjust = 0.5))

and that was similar to above, just center-justified and less space between text and border of plot.

If I drop the italics and just use \n, like this:

mytesttitle <- expression(paste("Summary of Days with\nWeather Suited, 2012-2021"))
ggplot(data=samp1)   geom_boxplot(aes(y=value)) 
 labs( y="Number of Days",
 title=mytesttitle)  
 theme(axis.text.x = element_blank(), axis.ticks.x=element_blank())
 

I obviously lack the italics. The title goes outside the viewable area as the following:

enter image description here

How do I get italics and a new line to work in the title?

CodePudding user response:

Your way of using atop actually works if you split the two parts of your phrase with a comma:

library(tidyverse)

mytesttitle <- expression(atop(
  paste("Summary of Days with ", italic(Weather)),
  "Suited with extra words to make this longer, 2012-2021"
))

ggplot(data=samp1)   geom_boxplot(aes(y=value)) 
  labs( y="Number of Days",
        title=mytesttitle)  
  theme(axis.text.x = element_blank(), axis.ticks.x=element_blank())

Another possible route would be to use plot.title = ggtext::element_markdown() to do italics and new line:


ggplot(data=samp1)   geom_boxplot(aes(y=value)) 
  labs( y="Number of Days",
        title="Summary of Days with *Weather*\n\nSuited with extra words to make this longer, 2012-2021")  
  theme(axis.text.x = element_blank(), axis.ticks.x=element_blank(),
        plot.title = ggtext::element_markdown())

CodePudding user response:

One posible solution is use subtitle to set your second part of title:

mytesttitle <- expression(paste("Summary of Days with", italic("Weather")))
ggplot(data=samp1)   geom_boxplot(aes(y=value))  
  #mdthemes::md_theme_classic()  
  ggtitle(mytesttitle, subtitle = "Suited with extra words to make this longer, 2012-2021")  
  theme(axis.text.x = element_blank(), axis.ticks.x=element_blank(),
        #title = element_text(size = 5),
        plot.title = element_text(hjust = 0.5, line = 0.6, size = 12)
        , plot.subtitle = element_text(hjust = 0.5, line = 0.6, size =12)
        )

enter image description here

  • Related