Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put more space between the axis labels and axis title in an R boxplot

Tags:

r

I am creating a boxplot in R with the following code:

boxplot(perc.OM.y ~ Depth, axes = F, ylim = c(-0.6, 0.2), xlim = c(3.5, 5.5),         lwd = 0.1, col = 8,          ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5) axis(1, at = c(3.5, 4, 5, 5.5), labels = c(" ", "Shallow", "Deep", " "),       cex.axis = 1.5) axis(2, cex.axis = 1.5) 

The problem is that the number labels on the y-axis currently overlap the axis title. Is there a way to put more space between the axis title and the axis number labels?

Thanks

like image 331
DQdlM Avatar asked Mar 31 '11 20:03

DQdlM


People also ask

How do I customize axis labels in R?

Key ggplot2 R functionsp + xlab(“New X axis label”): Change the X axis label. p + ylab(“New Y axis label”): Change the Y axis label. p + labs(x = “New X axis label”, y = “New Y axis label”): Change both x and y axis labels.

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 I increase axis label size?

You can change axis text and label size with arguments axis. text= and axis. title= in function theme() . If you need, for example, change only x axis title size, then use axis.


2 Answers

## dummy data dat <- data.frame(Depth = sample(c(3:6), 20, replace = TRUE), OM = 5 * runif(20)) 

Add some room for the y-axis labels and annotations, by making the margin bigger on the left hand side of the plot (side = 2):

## margin for side 2 is 7 lines in size op <- par(mar = c(5,7,4,2) + 0.1) ## default is c(5,4,4,2) + 0.1 

Now plot:

## draw the plot but without annotation boxplot(OM ~ Depth, data = dat, axes = FALSE, ann = FALSE) ## add axes axis(1, at = 1:4, labels = c(" ", "Shallow", "Deep", " "), cex.axis = 1.5) axis(2, cex.axis = 2) ## now draw the y-axis annotation on a different line out from the plot ## using the extra margin space: title(ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5,       line = 4.5) ## draw the box to finish off box() 

Then reset the plotting margins:

par(op) 

This gives:

boxplot

So we've created more space for the margin of the plot on side 2, and then drawn the axes and the annotation (ylab) separately to control how the plot is spaced out.

So the key to this is this line:

op <- par(mar = c(5,7,4,2) + 0.1) ## default is c(5,4,4,2) + 0.1 

What we do is save the original graphical parameters in object op, and change the margin sizes (in numbers of lines) to be 5, 7, 4, 2 + 0.1 lines each for the bottom , left, top, right margins respectively. The line above shows the defaults, so the code gives 2 more lines on the left margin than usually provided by default.

Then when we draw the y-axis label using title(), we specify which line (of the 7) to draw the label at:

title(ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5,       line = 4.5) 

Here I used line 4.5, but 5 would work also. The greater the values of 'line' the farther from the plot the label is drawn.

The trick is to find the value for the left margin and the value of 'line' in the title() call that allows the axis tick marks and the axis label to not overlap. Trial and error is likely the best solution to find the values you need with base graphics.

like image 166
Gavin Simpson Avatar answered Oct 12 '22 00:10

Gavin Simpson


Try setting the first value of mgp larger. You'll want to make the margins bigger too, with mar.

par(mgp=c(5,1,0)) par(mar=c(5,6,4,2)+0.1) 
like image 20
Aaron left Stack Overflow Avatar answered Oct 12 '22 01:10

Aaron left Stack Overflow