Removing top and right borders from boxplot frame in R
Asked Answered
S

3

16

Does anyone know how to remove the top and right borders of the boxplot frame in R? I have tried the argument frame=FALSE but that removes all sides but the left side(y-axis). I just want the x-axis and y-axis to display.

Thanks in advance!

Swollen answered 18/3, 2014 at 4:0 Comment(3)
You could add the frame back in with box(bty="l") once you have used frame=FALSEChristmastide
This is what I get when using bty command:Error in box(bty = "1") : invalid value specified for graphical parameter "bty"Swollen
That's a lowercase L ("l") not a one ("1").Christmastide
H
6

I think you need to use axis(side=1) after plotting.

x <- 1:5
boxplot(x, frame.plot = FALSE)
axis(side = 1)

This gives

enter image description here

Hickson answered 18/3, 2014 at 4:33 Comment(1)
This works, but the bottom border does not extend fully to the left. Also, I had specified xaxt="n" but the ticks come back with axis(side = 1). Any ideas how to remove the new ticks and extend bottom border to meet the y-axis? Thank you!Swollen
C
3
  1. Plot without axes
    boxplot(x, axes=F)
  1. Add a box of type "l" (that's an L, not a 1), like thelatemail suggested
    box(bty="l")
  1. Add axis ticks at desired values.
    axis(2)
    axis(1) #if you really want x-axis ticks here...

enter image description here

Crespo answered 4/6, 2020 at 2:31 Comment(0)
S
1

To remove ticks, you need to specify the ticks line width as zero (lwd.ticks=0). To ensure the x and y axes meet is a bit more laborious, you need to

  1. specify the lower limit of the y axis using ylim=...
  2. specify the height of the x axis using pos=...
  3. extend the x axis to the y axis - one way is simply to add a horizontal line using abline.

Putting that all together for the example above:

x <- 1:5
boxplot(x, frame.plot = FALSE,ylim=c(0,5))
axis(side=1, pos=0, lwd.ticks=0)
abline(h=0)

Barplot with frame removed

Stilliform answered 24/5, 2016 at 15:7 Comment(1)
This solution still doesnt look great, as you can clearly see where the shorter axes was (I wouldn't put this in a publication). This happens because the x-axes you add isn't precisely at zero. If you increase the line thickness of the added line, it looks a bit better, but still feels like a hacky solution: abline(h=0, lwd = 1.5)Taciturn

© 2022 - 2024 — McMap. All rights reserved.