Home > database >  plot boxplots from two datasets side by side
plot boxplots from two datasets side by side

Time:04-30

I have two data frames and I want to plot boxplots for scores beside each other and each data frame boxplot has a different color.

ID score1 score 2
1 200 300
2 300 150
3 400 -100
ID score1 score 2
200 200 300
300 300 150
400 400 -100

CodePudding user response:

This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format.

But fore reshaping the data, create a new column telling from which data set is this data and bind the two data sets.

x <- '
ID  score1  score2
1   200     300
2   300     150
3   400     -100'

y <- '
ID  score1  score2
200     200     300
300     300     150
400     400     -100'

df1 <- read.table(textConnection(x), header = TRUE)
df2 <- read.table(textConnection(y), header = TRUE)

dfboth <- rbind(
  cbind(data = 1, df1),
  cbind(data = 2, df2)
)

suppressPackageStartupMessages({
  library(dplyr)
  library(tidyr)
  library(ggplot2)
})

bind_rows(
  df1 %>% mutate(data = "1"),
  df2 %>% mutate(data = "2")
) %>%
  pivot_longer(starts_with("score"), names_to = "score") %>%
  ggplot(aes(data, value, fill = score))  
  geom_boxplot()  
  xlab("Data set")

Created on 2022-04-29 by the reprex package (v2.0.1)

  • Related