Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move axis labels ggplot

Tags:

r

ggplot2

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?

like image 939
Tyler Rinker Avatar asked Dec 10 '11 03:12

Tyler Rinker


People also ask

How do I move the Y-axis title in ggplot2?

For this purpose, we can use theme function of ggplot2 package. We would need to use the argument of theme function as axis. title. y=element_text(angle=0)) and this will write the Y-axis title to horizontal but the position will be changed to top.

How do I change axis labels in R?

To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.

How do I remove Y-axis labels in R?

When we create a plot in R, the Y-axis labels are automatically generated and if we want to remove those labels, the plot function can help us. For this purpose, we need to set ylab argument of plot function to blank as ylab="" and yaxt="n" to remove the axis title.

What is Hjust R?

The hjust argument is used to move the location of ggplot2 plot elements horizontally (hjust stands for horizontal adjustment). This example shows how to apply the hjust argument within the theme & element_text functions to move our main title to the middle of the plot.


2 Answers

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.

like image 156
Prasad Chalasani Avatar answered Sep 19 '22 18:09

Prasad Chalasani


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!

like image 23
EcologyTom Avatar answered Sep 19 '22 18:09

EcologyTom