Reputation: 701
EDIT: My original post (see below) was asking why vjust didn't shift the location of my title in a ggplot when I changed it from a string to an expression (so I could include some subscripts).
I have just tried running the original code and now vjust doesn't seem to move the location of my title whether its a string or an expression!
I am officially totally confused now! Why doesn't vjust work any more? Did I miss something?
Here's some code to show you what I mean...
ggplot(mtcars, aes(wt, mpg)) +
geom_line() +
theme_classic() +
theme(plot.title = element_text(size = rel(2), face="bold", vjust=-4)) +
labs(title="Title")
Maybe it will all make perfect sense after a good night's sleep...
ORIGNIAL POST:
I have trawled the internet for a number of hours and the closest solution to my problem that I have found is this...
ggplot2: Font Style in label expression
I have drawn some graphs using ggplot2 and (having done my research) have discovered not only how to use subscripted text in my titles (the original titles were just simple strings), but also how to make my (newly subscripted) titles appear in bold typeface.
I had previously used theme(plot.title = element_text(size=rel(2), vjust=-4, face='bold')
to adjust the size, location and weight of my titles, but now that the titles are presented as expressions (not strings) some parts of this don't seem to work any more.
As I said above - I have now resolved the 'bold' part. I'm pretty sure that the titles are still appearing in a larger size. However, the 'vjust' element doesn't seem to be working.
Any ideas on how I can adjust the vertical position of my titles now that they are expressions and not strings?
I'm on a PC, and have tried to run the code in a freshly opened R session - it didn't work.
Here's some example code to exemplify my point:
This works:
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
theme(plot.title = element_text(size = rel(2), vjust=-4)) +
labs(title="DDD")
This doesn't:
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
theme(plot.title = element_text(size = rel(2), vjust=-4)) +
labs(title=expression(paste(bold(D[s]), bold(-D[c]), bold(-D[r]))))
I guess I was really just hoping that there would be an alternative to theme(plot.title = element_text(size = rel(2), vjust=-4))
that I could use on an expression rather than a string that would allow me to shift the location of my plot titles (i.e., an alternative to either element_text or vjust).
Upvotes: 1
Views: 2046
Reputation: 77096
ggplot2 moves the y position of the title to oppose vjust, so it's not having any effect. You can tune the margin instead,
ggplot()+
theme(plot.title = element_text(vjust=-5,
debug=TRUE,
margin = margin(1*c(1,1,1,1), unit = "cm"))) +
labs(title="Title")
Upvotes: 1