Home > Software engineering >  How to group data by one variable while knowing (and plotting) the average and Q1,Q3 of other variab
How to group data by one variable while knowing (and plotting) the average and Q1,Q3 of other variab

Time:08-04

Here is the top few rows of my data with it's dput:

head(Actigraph)
  Participant.code Condition Day.within.condition Standing Sitting Stepping Cycling
1           AE1_01       PRE                    1     2676   25521      117      67
2           AE1_01       PRE                    2    17594   14793        6       8
3           AE1_01       PRE                    3     2174   29371      799      57
4           AE1_01       PRE                    4      395   32001        4       1
5           AE1_01       PRE                    5       72   31979       24     326
6           AE1_01       DBN                    1    11993   11129      875      64
dput(head(Actigraph))
structure(list(Participant.code = c("AE1_01", "AE1_01", "AE1_01", 
"AE1_01", "AE1_01", "AE1_01"), Condition = c("PRE", "PRE", "PRE", 
"PRE", "PRE", "DBN"), Day.within.condition = c(1L, 2L, 3L, 4L, 
5L, 1L), Standing = c(2676L, 17594L, 2174L, 395L, 72L, 11993L
), Sitting = c(25521L, 14793L, 29371L, 32001L, 31979L, 11129L
), Stepping = c(117L, 6L, 799L, 4L, 24L, 875L), Cycling = c(67L, 
8L, 57L, 1L, 326L, 64L)), row.names = c(NA, 6L), class = "data.frame")

I would like to group_by(Condition) and plot a box and whiskers for all 4 columns (Standing:Cylcing) PER condition.

So 4 plots (1 per condition), and within having 4 columns be displayed as box and whiskers

CodePudding user response:

We may reshape to 'long' format and then use ggplot

library(dplyr)
library(tidyr)
library(ggplot2)
Actigraph %>%
    pivot_longer(cols = Standing:Cycling) %>% 
    ggplot(aes(x = name, y = value, fill = Condition))   
     geom_boxplot()
  • Related