简体   繁体   中英

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

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

## 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:

箱形图

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.

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)

I just found this solution very straightforward and useful when I wanted to shrink the white space around the diagram (consider size limits in the conference papers!) while I wanted to avoid overlapping Y-axes title and big numbers as the ticks.

I use to set the titles as text and put them wherever I want, after setting the margins manually:

First, set the margins to the arbitrary values:

par( mar=c(m1, m2, m3, m4) ) 

where m1 to m4 are margins for four sides (1=bottom, 2=left, 3=top and 4=right).

For example:

par( mar=c(3.1, 4.7, 2.3, 0)) 

Then, when plotting, set main="", xlab="" and ylab="" (otherwise their text will overlap with this new text)

Finally, using mtext(), set the axis titles and diagram title manually:

mtext(side=1, text="X axes title", line=0.5)
mtext(side=2, text="Y axes title", line=3)
mtext(side=3, text="Diagram title", line=1.5)

The line parameter is the distance from the diagram (the smaller values puts it closer to the diagram).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM