Search code examples
rggplot2subsetboxplotcontinuous

How to separate boxplots in r?


I have to plot a boxplot for the PM2.5 levels when the rain > 0 for 12 noon. I’ve used the code:

boxplot(PM2.5~RAIN, data=subset(dat, RAIN > 0 & hour == 12), range = 0)

But it comes up with several boxplots (e.g. one boxplot for 0.1 mm rain, another for 0.2 mm rain)

Any help with separating these boxplots would be appreciated.


Solution

  • One way is to create a constant in the data and then use that on the right-hand side.

    dat$CONSTANT <- 0
    boxplot(PM2.5~CONSTANT, data=subset(dat, RAIN > 0 & hour == 12), range = 0)
    

    Another option is to create a subset first.

    with(subset(dat, RAIN > 0 & hour == 12),
        boxplot(PM2.5, range = 0) )