Search code examples
rscaleboxplot

How to change y-axis scale in a R boxplot function?


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


Solution

  • 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.