How to change y-axis scale in R boxplot function

13,969

Solution 1

library(datasets)
boxplot(cars[c('speed', 'dist')], col = "lightgray", ylim = range(0:120), yaxs = "i")
axis(4, at=seq(0, 120, 10))

enter image description here

The y-axis is on the right-hand side as you wanted I believe.

Solution 2

I am answering because the OP said in a comment that my comment did the job. I will also explain the code here.

There are two tricks to consider:

  1. First plot without the yaxis by setting argument yaxt = "n".
  2. Then plot the axis number 2, with the labels always perpendicular to the axis. This is done with las = 2.

So the final code is the following.

library(datasets)

boxplot(cars[c('speed', 'dist')],
        col = "lightgray", yaxt = "n")
axis(2, at = seq(0, 120, 10), las = 2)

enter image description here

Solution 3

You could use ggpubr instead. It let's you treat it as a gg object.

librabry(ggpubr)
library(reshape2)
df <- melt(cars)
p <- ggpubr::ggboxplot(data = df, x = "variable", y = "value", width = 0.8) +
  ggtitle("Plot of car") +
  xlab("my-xalabel") + ylab("my-ylabel")
>p

enter image description here

If you want in log scale:

p + ggpubr::yscale("log2", .format = TRUE)

enter image description here

Share:
13,969

Related videos on Youtube

claudius
Author by

claudius

Updated on June 04, 2022

Comments

  • claudius
    claudius almost 2 years

    When I do a boxplot diagram with the R boxplotfunction, this function prints the y-axis automatically.

    library(datasets)
    
    boxplot(cars[c('speed', 'dist')],
            col = "lightgray")
    

    In the ?boxplot I found the ylim parameter that change the y-axis limits, but not change the scale. So I tried to use the axis function to divide the scale from 0 to 120 every 10: axis(4, at = seq(0, 120, 10)). But I'm not getting a satisfactory result.

    I can't see where I'm making mistakes. Could someone help with this question?
    Thanks in advance.

    boxplot

    • Rui Barradas
      Rui Barradas over 4 years
      Maybe boxplot(..., yaxt = "n") followed by axis(2, at = seq(0, 120, 10), las = 2).
    • maydin
      maydin over 4 years
      This is a similar question which is supporting the @RuiBarradas 's comment.
    • claudius
      claudius over 4 years
      Hi @RuiBarradas it's worked fine! Thank you for your support. Could you make an answer, please? I didn't understand why las = 2 and not las = 1.
    • claudius
      claudius over 4 years
      Hi @maydin. I think this question is a little bit different. Because I could only make a segmented y-axis using the las argument in the axis function.
    • Rui Barradas
      Rui Barradas over 4 years
      Done, see the answer below.
  • Milo
    Milo over 4 years
    What does this do?
  • Vitali Avagyan
    Vitali Avagyan over 4 years
    correct implementation to put every 10 increments on vertical axes.
  • claudius
    claudius over 4 years
    Hi @MAPK, your answer didn't work for me. It's not about boxplot function. And did not return the boxplot diagram with the y-axis segmented every 10. Thank you for your support.
  • claudius
    claudius over 4 years
    Hi @Vitali Avagyan, your answer didn't work for me. How can you see plotting the diagram, its did not return the boxplot with the y-axis segmented every 10. Thank you for your support.
  • claudius
    claudius over 4 years
    I tried las = 1and it worked too. But las = 3 not work. I'm trying to understand how the las parameter as connected with the segmentation of y-axis. Looks like informagic.
  • Rui Barradas
    Rui Barradas over 4 years
    @claudius The reference is the official documentation: las = 1 means "always horizontal"; las = 2 means "always perpendicular to the axis and las = 3 is "always vertical". And it's working with me.
  • Vitali Avagyan
    Vitali Avagyan over 4 years
    That's strange, I was able to get it. I will try to add the result.
  • Vitali Avagyan
    Vitali Avagyan over 4 years
    @cladius, I have edited the post and put the result which was produced by a run of the same exact code that is written in my answer. Can you confirm at least that it is what you are after? Thanks.
  • claudius
    claudius over 4 years
    Now it worked fine. =) Thank you again for the support.