31

I have produced a fact graph in ggplot2 and the x axis title (bottom) is touching the scale values slightly (it's worsened when I plot to .pdf device). How do I move the axis title down a smidge?

DF<-structure(list(race = structure(c(3L, 1L, 3L, 2L, 3L, 1L, 2L, 
2L, 2L, 3L, 2L, 1L, 3L, 3L, 3L, 3L, 2L, 1L, 2L, 3L), .Label = c("asian", 
"black", "white"), class = "factor"), gender = structure(c(1L, 
1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 
2L, 2L, 2L), .Label = c("female", "male"), class = "factor"), 
    score = c(0.0360497844302483, 0.149771418578119, 0.703017688328021, 
    1.32540102136392, 0.627084455719946, -0.320051801571444, 
    0.852281028633536, -0.440056896755573, 0.621765489966213, 
    0.58981396944136, 1.95257757882381, 0.127301498272644, -0.0906338578670778, 
    -0.637727808028146, -0.449607617033673, 1.03162398117388, 
    0.334259623567608, 0.0912327543652576, -0.0789977852804991, 
    0.511696466039959), time1 = c(75.9849658266583, 38.7148843859919, 
    54.3512613852158, 37.3210772390582, 83.8061071736856, 14.3853324033061, 
    79.2285735003004, 31.1324602891428, 22.2294730114138, 26.427263191766, 
    40.5529893144888, 19.2463281412667, 8.45085646487301, 97.6770352620696, 
    61.1874163107771, 31.3727683430548, 99.4155144857594, 79.0996849438957, 
    21.2504885323517, 94.1079332400361)), .Names = c("race", 
"gender", "score", "time1"), class = "data.frame", row.names = c(NA, 
-20L))


require(ggplot2)
p <- ggplot(DF, aes(score, time1, group=gender))
p + geom_point(aes(shape=19)) + facet_grid(race~gender) + scale_x_continuous('BLAH BLAH') + 
scale_y_continuous('Some MOre Of theat Good Blahing') 

In my data BLAH BLAH is touching the numbers. I need it to move down. How?

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • Hmmmm. Can you expand a bit on what it looks like or post an example image, cause it seems to look fine when I run this. Might be specific to the platform/device/image size. – joran Dec 10 '11 at 04:44
  • @joran I couldn't post the image or the data because I'm using someone else's data. I couldn't get the problem to replicate outside of this data set though so I did the best I could. – Tyler Rinker Dec 10 '11 at 04:48
  • No problem...I see you got the pointer to adjusting the justification. I was just intrigued cause I couldn't replicate it. – joran Dec 10 '11 at 05:02

3 Answers3

42

You can adjust the positioning of the x-axis title using:

+ opts(axis.title.x = theme_text(vjust=-0.5))

Play around with the -0.5 "vertical justification" parameter until it suits you/your display device.

Prasad Chalasani
  • 19,912
  • 7
  • 51
  • 73
  • 102
    Remembering that opts and theme_text are deprecated these days. You should use theme(axis.title.x = element_text(vjust=-0.5)) – Eduardo Jun 12 '13 at 07:26
  • 2
    Is there an equivalent that changes the positioning horizontally, while preserving the arrangement of the text? hjust seems to change the text justification and I'd like to just change the location. – kennyB Mar 07 '16 at 20:36
  • 19
    Since at least ggplot 2.0.0 the vjust parameter has been superseded by [margins](https://github.com/tidyverse/ggplot2/issues/1435). Margins can be specified for top (t), right (r), bottom (b), and left (l) to position text vertically and horizontally. So to move text down you would use `theme(axis.title.x = element_text(margin = margin(t = 20))`. – JWilliman Nov 06 '16 at 22:10
12

This is an easy workaround, based on the answer provided here

Just add a line break; \n, at the start of your axes title; xlab("\nYour_x_Label") (Or at the end if you need to move your y label).

It doesn't offer as much control as Eduardo's suggestion in the comments; theme(axis.title.x = element_text(vjust=-0.5)), or use of margin, but it is much simpler!

EcologyTom
  • 2,344
  • 2
  • 27
  • 38
2

I would like to note that this is not my answer but @JWilliman - their answer is in the comments on @Prasad Chalasani answer. I am writing this as the current upvoted answers did not actually work well for me but @JWilliman's solution does:

#Answer   
+ theme(axis.title.x = element_text(margin = margin(t = 20))

This is because theme(axis.title.x = element_text(vjust = 0.5)) has been superseded and now moves the title/label a fixed distance regardless of the value you put in.

halfer
  • 19,824
  • 17
  • 99
  • 186
André.B
  • 617
  • 8
  • 17