Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a title to a ggplot when the title is a variable name?

Tags:

r

ggplot2

title

At the end of a ggplot, this works fine:

+ opts(title = expression("Chart chart_title...")) 

But this does not:

chart_title = "foo" + opts(title = expression(chart_title)) 

nor this:

chart_title = "foo" + opts(title = chart_title) 

How can I add a title to a ggplot when the title is a variable name?

like image 552
John Avatar asked Mar 01 '10 22:03

John


People also ask

How to add a title in ggplot in R?

Adding a title 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.

How do I change the size of my Ggtitle?

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. title parameter for doing changes in the title of plot and plot.


1 Answers

Opts is being deprecated. One option is to use labs()

myTitle <- "My title" qplot(mpg, wt, data = mtcars) + labs(title = myTitle) 

Pretty much the same.

like image 111
Greg Avatar answered Sep 27 '22 20:09

Greg