Reputation: 23
I can't seem to wrangle the following data:
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
Upvotes: 0
Views: 80
Reputation: 887128
In base R
, it is easier
boxplot(df1)
df1 <- structure(list(Year2020 = c(275L, 262L, 261L, 261L, 257L, 255L,
254L, 253L, 253L), Year2019 = c(274L, 274L, 271L, 267L, 266L,
265L, 265L, 265L, 264L), Year2018 = c(269L, 267L, 264L, 262L,
261L, 261L, 260L, 259L, 258L)), class = "data.frame", row.names = c(NA,
-9L))
Upvotes: 0
Reputation: 111
You can also use reshape2::melt(data) to concert your data from wide to long form and then use ggplot
Upvotes: 0
Reputation: 6206
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()
Upvotes: 2