1

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).

Community
  • 1
  • 1
Psychologeek
  • 641
  • 1
  • 5
  • 8
  • 1
    Your question does not contain a [reproducible example](http://stackoverflow.com/q/5963269/4303162). It is therefore hard to understand your problem and give you an appropriate answer. Please make your data available (e.g. by using `dput()`) or use one of the example data sets in R. Also, add the minimal code required to reproduce your problem to your post. – Stibu Mar 17 '16 at 16:56
  • The titles are formatted using `element_text` function which has margin argument where you can write `unit(rel(0.5), 0, 0, 0, units = "pt")` essentially for increasing the distance of title from plot. You can play with it further. – TheRimalaya Mar 17 '16 at 17:14
  • @TheRimalaya that looks like it might work, but can you provide a little more detail please? Maybe a line of code would help? theme(plot.title = element_text(...))? – Psychologeek Mar 17 '16 at 17:50
  • 1
    If you want to get a little help, you need to make your code [reproducbile](http://stackoverflow.com/q/5963269/4303162). Anyone should be able to copy your code and run it as it is in R. This is not possible now. For example, `Ylimit` is unknown. Without runable code, we are limited to guesswork and it is hard to be more specific than TheRimalaya already was. – Stibu Mar 17 '16 at 19:30
  • OK, sorry. I'm quite new to StackOverflow (at least in terms of posting questions). I'll add in the extra bits necessary to run the code, but the graphs are based on 40 data files each containing ~1200 lines of data and there is a considerable amount of data processing done before the graphs get drawn. I'm not trying to sideline the issues here, but it seems clear that my original question (including the code) isn't enough to go on, so I guess a quick fix is out of the question. I just hoped there was an alternative to element_text that I could use with expressions rather than strings. – Psychologeek Mar 17 '16 at 20:41
  • You don't need to show everything. A **minimal** reproducible example is perfect. Share *just enough* code and data so that we can see the problem. Since your issue is about the plot title I would think you could just use `ggplot(mtcars, aes(wt, mpg)) + geom_point()` and add a title with an expression to illustrate the problem. – Gregor Thomas Mar 17 '16 at 20:55
  • OK, gotcha - let me try again... – Psychologeek Mar 17 '16 at 21:18

1 Answers1

1

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") 
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Thanks baptiste, I've now had a play around with `margin` which seems to change the shape of the entire plot (including the title) whereas what I'm trying to achieve is to keep the plot the same size and shape but shift the title to a position just below the top of the y-axis. When I used `vjust` yesterday it worked fine (well, when the title was a string at least), but now `vjust` doesn't seem to do anything to the position of the title at all (whether its a string or an expression). As you said - I want to move the y position of the title using `vjust` but now it doesn't work - any ideas? – Psychologeek Mar 18 '16 at 06:54
  • I don't know, the margin seems to work. It would help if you edited your question describing where exactly you want the title to be. – baptiste Mar 18 '16 at 08:50
  • OK, so it turns out that I hadn't tried playing with each of the parameters in `margin`. A little more experimentation has resolved my problem - thanks again =] – Psychologeek Mar 18 '16 at 08:54
  • @baptiste When I implement your solution a weird yellow background strip appears horizontally on the location of my `ggtitle`. How can I remedy this? I'm trying to shift the title into the upper right hand corner within the plot. 'hjust` allows me to shift the the title to the right and your solution works to vertically shift the title within the plot. However, your solution leaves a yellow background strip going horizontally in my plot distorting the figures. Any help on how to fix this would be appreciated. – Meli Jun 28 '16 at 14:46
  • @Meli try `debug=FALSE` – baptiste Jun 29 '16 at 08:43