Making a plot with multiple boxplots, but with columns as the factors

Viewed 36

I'm having some trouble with this conceptually.

Using the 'Decathlon' dataset from library(GDAdata) and ggplot2;

I want to make boxplots using all of the point variables, which is essentially the last half of the dataset; columns = 15:24 or P100m:P1500.

On a single plot, I want a boxplot for each of those columns on the x axis, and Totalpoints on the y axis.

Any help is appreciated! Thank you.

1 Answers

Here is one way to do it:

  1. Load GDAdata::Decathlon
  2. Make long format with pivot_longer
  3. then use ggplot with geom_boxplot()
library(tidyverse)
library(GDAdata)
df <- GDAdata::Decathlon %>% 
  select(15:24) %>% 
  pivot_longer(
    cols = everything(),
    names_to = "Names",
    values_to = "Values"
  )

p <- ggplot(df, aes(x=Names, y=Values)) + 
  geom_boxplot()
p

enter image description here

Related