pipts
pipts

Reputation: 87

How to create box plots by group?

I have this df and I am trying to create a box plot for every ID. Ideally, I want all the boxplots on the same page in order to be able to compare them. Can anyone help?

dput(df)
structure(list(ID = c("F1", "F1", "F1", "F1", "F1", "F1", "F1", 
"F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F3", "F3", "F3", 
"F3", "F3", "F3", "F3", "F3", "F3", "F4", "F4", "F4", "F4", "F4", 
"F4", "F4", "F4"), Date = c("22/6/2021", "23/6/2021", "24/6/2021", 
"25/6/2021", "26/6/2021", "27/6/2021", "28/6/2021", "22/6/2021", 
"23/6/2021", "24/6/2021", "25/6/2021", "26/6/2021", "27/6/2021", 
"28/6/2021", "29/6/2021", "22/6/2021", "23/6/2021", "24/6/2021", 
"25/6/2021", "26/6/2021", "27/6/2021", "28/6/2021", "29/6/2021", 
"30/6/2021", "22/6/2021", "23/6/2021", "24/6/2021", "25/6/2021", 
"26/6/2021", "27/6/2021", "28/6/2021", "29/6/2021"), Values = c(9.6, 
9.8, 10.2, 9.8, 9.9, 9.9, 9.9, 1.2, 1.2, 1.8, 1.5, 1.5, 1.6, 
1.4, 1.1, 3266, 3256, 7044, 6868, 6556, 3405, 3410, 3980, 5567, 
59.4, 56, 52.8, 52.4, 55.5, 54, 61, 53.6)), class = "data.frame", row.names = c(NA, 
-32L))

Upvotes: 0

Views: 86

Answers (1)

David
David

Reputation: 10152

As Lucas pointed out, facets are one option. If you want to see the boxplots side by side, you can also use fill = ID like so:

library(ggplot2)


df <- structure(list(ID = c("F1", "F1", "F1", "F1", "F1", "F1", "F1", 
                            "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F3", "F3", "F3", 
                            "F3", "F3", "F3", "F3", "F3", "F3", "F4", "F4", "F4", "F4", "F4", 
                            "F4", "F4", "F4"), 
                     Date = c("22/6/2021", "23/6/2021", "24/6/2021", 
                              "25/6/2021", "26/6/2021", "27/6/2021", "28/6/2021", "22/6/2021", 
                              "23/6/2021", "24/6/2021", "25/6/2021", "26/6/2021", "27/6/2021", 
                              "28/6/2021", "29/6/2021", "22/6/2021", "23/6/2021", "24/6/2021", 
                              "25/6/2021", "26/6/2021", "27/6/2021", "28/6/2021", "29/6/2021", 
                              "30/6/2021", "22/6/2021", "23/6/2021", "24/6/2021", "25/6/2021", 
                              "26/6/2021", "27/6/2021", "28/6/2021", "29/6/2021"),
                     Values = c(9.6, 
                                9.8, 10.2, 9.8, 9.9, 9.9, 9.9, 1.2, 1.2, 1.8, 1.5, 1.5, 1.6, 
                                1.4, 1.1, 3266, 3256, 7044, 6868, 6556, 3405, 3410, 3980, 5567, 
                                59.4, 56, 52.8, 52.4, 55.5, 54, 61, 53.6)), 
                class = "data.frame", row.names = c(NA, 
                                                    -32L))


ggplot(df, aes(x = ID, y = Values, fill = ID)) +
  geom_boxplot() +
  scale_y_log10()

enter image description here

NOTE: I scaled the y values (log10) to be able to see differences in F1, F2, and F4 better.

Upvotes: 3

Related Questions