How to change the vertical position of the ggplot header without changing the alignment of the axis label

Edit: The problem was that I incorrectly tried to change theme(title = element_text()) when I needed to change theme(plot.title(element_text()) . I would have noticed that if I had studied the theme() documentation more carefully theme() .

Original post:

Changing the vertical alignment of the header also changes the position of the x and y axis labels. This is mistake? Or am I misinterpreting the theme () function? 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 schedule 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 also move and fuzzy plot the graph.

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

enter image description here

+7
r ggplot2
source share
2 answers

You want plot.title not title :

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

An alternative quick fix adds a line break:

  labs(title="My Nice Graph\n") 
+24
source share

vjust doesn't work for me (I also think the values ​​should be in [0, 1]). I use

 ... + theme( plot.title = element_text(margin=margin(b = 10, unit = "pt")) ) 
+6
source share

All Articles