0

I am new to R and have created a boxplot, however my x axis labels are too long.

Is there a way to move the boxplot upwards WITHOUT using ggplot2 as I need this boxplot to be consistent looking with my other graphs.

I also need to add an x axis label and I'm aware of the mtext(...) to add the label instead, however when I have tried this previously it flashes on the screen and disappears.

I will attach my current code without the x axis title or text, just the x axis labels being to long.

    ana2$B<-factor(ana2$Burn,c("N","L","S"))
    with(ana2,boxplot(Time~ana2$B*Graze, ylab= "Infiltration Rate (mm/h) " , xlab=" ", names=c("1954 burn-Grazed", "Long interval-Grazed", "Short interval-Grazed", "1954 burn-Ungrazed", "Long interval-Ungrazed",  "Short interval-Ungrazed"), cex.axis=0.75, ylim=c(0, 80000 ), las=2 ))

X axis too long for boxplot

BelleT
  • 33
  • 2
  • 10
  • Do you just want the boxes to take up more of the plot vertically? If so, just adjust your `xlim=` argument. – thelatemail Feb 08 '18 at 23:00
  • I want to move the whole graph upwards so the labels are visible and I can add an x axis title – BelleT Feb 08 '18 at 23:14
  • You could either make the labels smaller (or angle them differently) or increase the bottom margin using `mar` under the `par()` options. – joran Feb 08 '18 at 23:19
  • Thanks I have made them visible, however as they are still long when I add the xlab label it overlaps. How can I edit this?? – BelleT Feb 08 '18 at 23:34
  • @BelleT - maybe consider adding some line-breaks `\n` in the labels, e.g. `"This is a long\nlabel split in half"` – thelatemail Feb 09 '18 at 00:09
  • 2
    Is this the same or very similar question from the same person? Both seem to ask about moving x-axis up. https://stackoverflow.com/questions/48674026/r-box-plot-how-to-move-x-and-y-axis-to-be-visible/48674261 – kangaroo_cliff Feb 09 '18 at 00:27

1 Answers1

0

I've modified the plot to tilt the (modified) labels (as @joran suggested in the comments). The Ungrazed/Grazed portions are labeled separately. This is partly based on an answer to a query 4+ years ago from @Henrik (r boxplot tilted labels x axis). I've used made-up data and a made-up structure for the data frame. Room for an x-axis label, as well.

labels=c("1954 burn", "Long interval",
     "Short interval", "1954 burn",
     "Long interval",  "Short interval")
par(mar=c(4,5,1,2))
boxplot(Time~Graze, ylim=c(0,80000),
    ylab= "Infiltration Rate (mm/h) ", axes=F,
    xlab=" ", data=ana2,
    names=rep('',6),
    las=2 )
axis(2,at=seq(0,80000,20000), labels=c('0','20k','40k','60k','80k'),las=1)
# place x labels
text(x=c(1:6)+.1, y=-5000, cex=.7, srt = 30, adj = 1.1,
 labels = labels, xpd = TRUE)
box()
text(2,70000,'Grazed', cex=.8)
text(5,70000,'Ungrazed', cex=.8)
text(3.5,-30000,'Condition')

enter image description here

Edward Carney
  • 1,372
  • 9
  • 7