Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplot2 center align a multi-line title

Tags:

If I have a title that goes

... + ggtitle('Something\nSomething Else\nSomething Else') 

Is there any way I can get each line to center align rather than left align in the center?

...+ theme(plot.title=element_text(hjust=0.5)) 

gives me text in the center, but left aligned.

like image 659
Tahnoon Pasha Avatar asked Apr 18 '13 04:04

Tahnoon Pasha


People also ask

How do I make Ggtitle bold?

Change the font appearance (text size, color and face) of titles and caption. For example, to set a bold ggplot title, use this: p + theme(plot. title = element_text(face = "bold")) . The allowed values for the font face include: “plain”, “italic”, “bold” and “bold.

How do I make my Ggplot title bigger?

To change the size of the title and subtitle, we add the theme() function to labs() or ggtitle() function, whatever you used. Here we use labs() function. Inside theme() function, we use plot.

How do I add a title to a Ggplot in R?

To add a title to your plot, add the code +ggtitle("Your Title Here") to your line of basic ggplot code. Ensure you have quotation marks at the start and end of your title. If you have a particulary long title that would work better on two lines, use \n for a new line.


1 Answers

would this work for you,

# install.packages("ggplot2", dependencies = TRUE) require(ggplot2)  DF <- data.frame(x = rnorm(400)) m <- ggplot(DF, aes(x = x)) + geom_histogram() m + labs(title = "Vehicle \n Weight-Gas \n Mileage Relationship \n                   and some really long so that you can seee it's centered") +       theme(plot.title = element_text(hjust = 0.5)) 

enter image description here

sorry about the typos in the plot title …

like image 179
Eric Fail Avatar answered Sep 22 '22 13:09

Eric Fail