Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change vertical position of ggplot title without altering axis label justification

Tags:

r

ggplot2

Edit: The problem was due to my incorrectly attempting to alter theme(title = element_text()), when I needed to alter theme(plot.title(element_text()). I would have noticed this had I more carefully scrutinized the theme() documentation.

Original Post:

Changing the title vertical justification alters the position of the x and y axis labels as well. Is this a bug? Or am I misinterpreting the function of theme()? I am running ggplot2 version 0.9.3.1

Minimal reproducible example.

require(ggplot2)
set.seed(12345)
x <- rnorm(100,10,0.5)
y <- x * 3 + rnorm(100)
df <- data.frame(y,y)

The default title is too close to the graph for my taste....

ggplot(df,aes(x,y)) + 
geom_point() + 
labs(title="My Nice Graph")

enter image description here

When I try to move the title, the axis labels move also, and illegibly plot on the graph.

ggplot(df,aes(x,y)) + 
geom_point() + 
labs(title="My Nice Graph") + 
theme(title = element_text(vjust=2))

enter image description here

like image 601
Andrew Barr Avatar asked Oct 23 '13 20:10

Andrew Barr


1 Answers

You want plot.title not title:

labs(title="My Nice Graph") + theme(plot.title = element_text(vjust=2))

Alternate quick fix is adding a line break:

  labs(title="My Nice Graph\n")
like image 85
Ricardo Saporta Avatar answered Oct 12 '22 19:10

Ricardo Saporta