Creating boxplot in R with ggplot2 with different x-axis groups

Md. Ahsan :

I can't seem to wrangle the following data:

https://i.stack.imgur.com/r4w6j.png

Year 2020   Year 2019   Year 2018
275 274 269
262 274 267
261 271 264
261 267 262
257 266 261
255 265 261
254 265 260
253 265 259
253 264 258

so that the x axis shows the Year of each box plot and y axis shows the values. Each column is about 50 items long representing 50 different student-scores for that year

sahwahn :

I removed the whitespace from the column names.

First, it's best to use tidyr::pivot_longer to convert the data in long form, as ggplot2 is designed to work with long form, rather than wide form data.

If you have a lot of columns, you can replace the cols=c("Year_2020", "Year_2019", "Year_2018") to everything()

    dat %>% 
tidyr::pivot_longer(cols=c("Year_2020", "Year_2019", "Year_2018"), names_to="year") %>% 
ggplot2::ggplot(aes(x=year, y=value)) + geom_boxplot()

enter image description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=404858&siteId=1