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

John   picture John · Mar 1, 2010 · Viewed 60.5k times · Source

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?

Answer

Greg picture Greg · Dec 30, 2012

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.