Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R base plot, move axis label closer to axis

I have eliminated labels on the y axis because only the relative amount is really important.

w <- c(34170,24911,20323,14290,9605,7803,7113,6031,5140,4469) plot(1:length(w), w, type="b", xlab="Number of clusters",      ylab="Within-cluster variance",      main="K=5 eliminates most of the within-cluster variance",      cex.main=1.5,      cex.lab=1.2,      font.main=20,      yaxt='n',lab=c(length(w),5,7), # no ticks on y axis, all ticks on x      family="Calibri Light") 

cluster plot

However, suppressing those tick labels leaves a lot of white space between the y axis label ("Within-cluster variance") and the y axis. Is there a way to nudge it back over? If I somehow set the (invisible) tick labels to go inside the axis, would the axis label settles along the axis?

like image 406
C8H10N4O2 Avatar asked May 15 '15 17:05

C8H10N4O2


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 Y axis position in R?

The easiest way to change the Y-axis title in base R plot is by using the ylab argument where we can simply type in the title. But the use of ylab does not help us to make changes in the axis title hence it is better to use mtext function, using which we can change the font size, position etc.


1 Answers

Try setting ylab="" in your plot call and use title to set the label of the y-axis manually. Using line you could adjust the position of the label, e.g.:

plot(1:length(w), w, type="b", xlab="Number of clusters", ylab="",      main="K=5 eliminates most of the within-cluster variance",      cex.main=1.5,      cex.lab=1.2,      font.main=20,      yaxt='n',lab=c(length(w),5,7), # no ticks on y axis, all ticks on x      family="Calibri Light")  title(ylab="Within-cluster variance", line=0, cex.lab=1.2, family="Calibri Light") 

enter image description here

Please read ?title for more details.

like image 168
sgibb Avatar answered Oct 11 '22 14:10

sgibb