0
ana1$B<-factor(ana1$Burn,c("C","N","L","S"))
with(ana1,boxplot(Time~ana1$B, ylab  = "Infiltration Rate (mm/h) " , xlab ="Burn Treatment", las = 2, par(mar = c(12, 5, 4, 2)+0.1), names = c( "Control" , "Not burned since 1954" , "Long rotation burn" , "Short rotation burn" ) ) )

Hi, this is my current code, which creates axis titles that are obstructed by my rotated labels. Does any one know how to move both of the axis titles across and down?

I am really struggling with this, although the solution may be very easy!

enter image description here

So I have alerted the code, however now the x axis label has gone off the box plot entirely

    ana1$B<-factor(ana1$Burn,c("C","N","L","S"))
    with(ana1,boxplot(Time~ana1$B, ylab  = "Infiltration Rate (mm/h) " , xlab = "", las = 2, par(mar = c(12, 4, 4, 1)+0.1), title("Burn Treatment", line = 10), names = c( "Control" , "Not burned since 1954" , "Long rotation burn" , "Short rotation burn" ) ) )

The parameter has changed but the x axis label has disappeared

Updated boxplot

BelleT
  • 33
  • 2
  • 10

1 Answers1

0

You can use title to manually draw an axis title, and adjust its position through the line parameter. For example, for the x axis, you can do

par(mar = c(12, 4, 4, 2) + 0.1);
boxplot(df, xlab = "", las = 2);
title(xlab = "Burn Treatment", line = 10);

enter image description here

You can increase the plot margin using par(mar = ...). You'll probably need to tweak the parameters par(mar = ...) and title(..., line = ...) for your data.


Sample data

df <- data.frame(
    'Control' = rnorm(100),
    'Not burned since 1954' = rnorm(100),
    'Long rotation burn' = rnorm(100),
    'Short rotation burn' = rnorm(100))

Update

The following should work with your data:

par(mar = c(12, 4, 4, 1) + 0.1);       # This sets the plot margins
with(                                  # Draw the boxplot
    ana1,
    boxplot(
        Time ~ B, 
        ylab  = "Infiltration Rate (mm/h) " , 
        xlab = "", 
        las = 2, 
        names = c("Control" , "Not burned since 1954" , "Long rotation burn" , "Short rotation burn")));
title(xlab = "Burn Treatment", line = 10);    # Add x axis title

You might have to play around with line = 10 and try different values; ditto for the plot margins. The first number in mar = c(12, 4, 4, 2) gives the bottom margin, see ?par for details.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • hi, so I am very new to r and I understand what you mean but I am not able to format this into my own code without it saying error. – BelleT Feb 07 '18 at 23:23
  • ana1$B<-factor(ana1$Burn,c("C","N","L","S")) with(ana1,boxplot(Time~ana1$B, ylab = "Infiltration Rate (mm/h) " , xlab = "Burn Treatment", las = 2, par(mar = c(12, 4, 4, 2)+0.1), boxplot(df, xlab = "", las = 2) title(xlab = "Burn Treatment", line = 10), names = c( "Control" , "Not burned since 1954" , "Long rotation burn" , "Short rotation burn" ) ) ) – BelleT Feb 07 '18 at 23:23
  • that is how I have written the new code, however I am sure I have gone wrong and gotten confused as it says error every time I run it through R – BelleT Feb 07 '18 at 23:24
  • @BelleT Please don't put extended code in comments; it makes it very difficult/impossible to read. You can edit your original question to include your sample code and sample data. – Maurits Evers Feb 07 '18 at 23:27
  • sorry I am new to this website also. I have edited my original question and since altered the code, so now it does not say there is an error, it just makes the x axis label disappear! I appreciate your help, I am finding R rather challenging and have been asked to give it a go for a university project – BelleT Feb 07 '18 at 23:41
  • @BelleT No need to apologise; I've edited my solution, please take a look. There are some great resources available on SO (for example how to provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), including sample data) that will help you improve the quality of future questions, and attract more attention. – Maurits Evers Feb 08 '18 at 00:35
  • If I copy this into R should it work? or do I need to do something to it? at the moment when I run it, the only axis label appearing is the y axis which is overlapped – BelleT Feb 08 '18 at 00:48
  • @BelleT Ah sorry; I made a mistake in the update; it should have been `title(xlab = "...", line = 10)` just like in the first bit of my solution. – Maurits Evers Feb 08 '18 at 00:56
  • @BelleT Did this solve your problem? If so, you can close the question by setting the tick mark next to the answer. – Maurits Evers Feb 08 '18 at 21:58