0

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

Jonas
  • 49
  • 7

2 Answers2

1
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

sveer
  • 427
  • 3
  • 16
0

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

manro
  • 3,529
  • 2
  • 9
  • 22
  • Yes, thats the plot. But instead of the id on the x-axis I would like to have the columns names (duration1, duration2, duration3) there and all three in one plot. – Jonas Oct 23 '21 at 11:21
  • @Jonas I will correct, but you can see an answer above... – manro Oct 23 '21 at 11:22