1

Here's a sample of my data df

group   score1  score2
a   12  15
a   11  14
a   24  22
b   34  24
b   14  23
b   33  44
c   11  22
c   23  34
c   32  43
...

I know how to use ggplot2 to make a boxplot comparing group with score1 and another boxplot comparing group with score2.

But how do I make one boxplot where X axis is group, with boxes for both score1 and score2?

0

1 Answer 1

3

I believe this is probably what you're getting at -- first you might want to consider tidying the dataset with tidyr, then you can make the plot using a call to interaction():

library(ggplot2)
library(tidyr)

df <- gather(df, score, value, -group)
head(df)
#   group  score value
# 1     a score1    12
# 2     a score1    11
# 3     a score1    24
# 4     b score1    34
# 5     b score1    14
# 6     b score1    33

ggplot(df, aes(x = interaction(score, group), y = value)) +
  geom_boxplot()

Plot 01

As an alternative, you could pass the new score variable to a color aesthetic to get something like this:

Plot 02

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.