Todd Burus
Todd Burus

Reputation: 983

Multiple boxplots from conditions on a single variable

I am looking for simplified way to create multiple boxplots from conditions on a single variable in Base R. I know how to do this by creating a new column and using a formula, but would like a way to do it solely within the boxplot() function (i.e. in one step) if possible.

For instance, say my data looks like:

     R BatAge
1  614   26.8
2  602   27.9
3  613   27.8
4  654   29.3
5  685   29.4
6  707   29.9
7  855   30.1
8  752   29.3
9  716   28.6
10 703   29.7
11 789   30.1
12 724   31.3
13 706   30.2
14 777   30.7
15 764   30.8
16 747   31.2
17 831   30.1
18 687   29.4
19 772   29.0
20 693   28.3

Is it possible for me to separate the 'R' column into two boxplots based on whether or not BatAge < 30 by just using the boxplot() function and not having to create a variable that sorts them beforehand?

Upvotes: 0

Views: 1120

Answers (2)

neilfws
neilfws

Reputation: 33782

You're going to have to use some function outside of boxplot to generate a categorical variable.

One way in base R using Hmisc::cut2, assuming data frame named mydata:

boxplot(R ~ Hmisc::cut2(BatAge, 30), mydata)

enter image description here

Or using dplyr::mutate()

library(dplyr)
mydata %>% 
  mutate(Group = ifelse(BatAge >= 30, "High", "Low")) %>% 
  boxplot(R ~ Group, .)

Upvotes: 0

Humpelstielzchen
Humpelstielzchen

Reputation: 6441

This should work:

boxplot(R ~ BatAge > 30, data = df)

enter image description here

Upvotes: 3

Related Questions