Jonas
Jonas

Reputation: 79

R create boxplot based on columns data

I have a table with the following columns:

id duration1 duration2 duration3
1 500 250 140
2 300 270 200
3 250 200 300
4 400 210 400

I want to plot the values of duration1, duration2, and duration3 in a boxplot with the column names (duration1,...2,...3) as labels of the x-axis and their column values as y-vales.

Example image

Upvotes: 0

Views: 75

Answers (2)

manro
manro

Reputation: 3677

Do you want something like this?

library(tidyverse)
 data1 <- tibble(
        id = 1:4,
        duration1 = c(500,300,250,400),
        duration2 = c(250,270,200,210),
        duration3 = c(140,200,300,400)
 )
  data1 <- data1 %>%
     pivot_longer(-id)

  boxpl <- ggplot(data1, aes(x=name, y=value, fill = name)) + 
     geom_boxplot() + theme_classic()
    

enter image description here

Upvotes: 0

sveer
sveer

Reputation: 472

dat <- data.frame('id' = c(1,   500,    250,    140), 
                  'duration1' = c(2,    300,    270,    200), 
                  'duration2' = c(3,    250,    200,    300), 
                  'duration3' = c(4,    400,    210,    400), 
                  stringsAsFactors = F) 

dat_plot <- dat %>%
         pivot_longer(-id)
 
 p <- ggplot(dat_plot)+   geom_boxplot(aes(x = name, y = value, fill = name)) 
 p

required output reference

Upvotes: 1

Related Questions