R: Box Plot - How to move x and y axis to be visib

2019-08-30 03:45发布

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

标签: r
1条回答
欢心
2楼-- · 2019-08-30 04:28

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.

查看更多
登录 后发表回答