Home > front end >  Boxplots from frequency columns in ggplot2
Boxplots from frequency columns in ggplot2

Time:12-12

I have a dataframe such as the example below, which describes the number of students achieving specific scores (25-100) in each class (a,b,c)

df
#   score class_a class_b class_c
# 1    25       0      10       5
# 2    50       5       3       7
# 3    75       2       2       2
# 4   100       0       6       4

I would like to create a box blot with class on the x axis, and the scores as the y axis, in order to show the range of scores for each class.

But, I am really not sure how to do this with summarized data such as this. I have tried:

library(reshape2)

df1 <- melt(df, id.vars='score')

But I am not sure this is the right direction.

Data

df <- data.frame(score=c(25, 50, 75, 100), class_a=c(0, 5, 2, 0), 
                 class_b=c(10, 3, 2, 6), class_c=c(5, 7, 2, 4))

CodePudding user response:

You may repeat the scores according to the frequencies in each class and boxplot the list.

Map(rep.int, df[1], df[-1]) |> boxplot()

enter image description here

  • Related