Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: how to move axes labels closer to the plot

I would like to move the axes labels closer to my plot. How can I do that?

set.seed(3)
plot(rnorm(10), xlab = "Age", ylab = "Weight", cex.lab = 1.5)

enter image description here

like image 371
Adrian Avatar asked Jun 11 '15 23:06

Adrian


People also ask

How do you adjust axes in R?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

How do I change the size of axis labels in R?

Go to the menu in RStudio and click on Tools and then Global Options. Select the Appearance tab on the left. Again buried in the middle of things is the font size. Change this to 14 or 16 to start with and see what it looks like.

How do you set the axis labels and title of the R plots?

Change main title and axis labelsmain: the text for the main title. xlab: the text for the x axis label. ylab: the text for y axis title. sub: sub-title; It's placed at the bottom of x-axis.


2 Answers

I think the command you're looking for is:

par(mgp=c(2,1,0))  

Then create your plot:

plot(rnorm(10), xlab = "Age", ylab = "Weight", cex.lab = 1.5)

Once you're done you can reset it to the default:

par(mgp=c(3,1,0))
like image 133
Adam C Avatar answered Sep 24 '22 06:09

Adam C


Using title() and specifying line should work for you. You may also want to consider changing the margins via par(oma = c()) or par(mar = c()) (the sequence of numbers goes: bottom, left, top, right).

set.seed(3)
plot(rnorm(10), xlab = "Age", ylab = "", cex.lab = 1.5)
title(ylab = "Weight", line = 2, cex.lab = 1.5)

Hope it helps

like image 44
Daniel Anderson Avatar answered Sep 24 '22 06:09

Daniel Anderson