Search code examples
rlinemeandiagram

R mean sorted by groups plot line diagram with ggplot


I have some data like this. At the end there will be 20 trainings.

Intensitaet.Auswertung <- data_Auswertung [, c("Type", "training1", "training2", "training3", "training4", "training5")] 

    group                   `training1`    `training2`    `training3`    `training4`    `training5`
   <chr>                       <dbl>          <dbl>          <dbl>          <dbl>          <dbl>
 1 Group1                        6              6              4              2              8
 2 Group1                        4              5              5              5              7
 3 Group2                        5              3              3              3              6
 4 Group1                        5              3              4              3              6
 5 Group1                        5              7              8              6              7
 6 Group2                        4              3              4              5              7
 7 Group2                        5              5              5              5              7
 8 Group1                        7              8              6              5              8
 9 Group2                        3              4              4              4              8
10 Group2                        6              5              4              5              4
# ... with 11 more rows

I want to plot a line diagram with the mean of every training, soreted by groups.

I hope somebody can help me, im very new in R.


Solution

  • Do you mean something like this?

    library(tidyverse)
    Intensitaet.Auswertung %>% 
        group_by(group) %>% 
        summarise(across(everything(), mean)) %>% 
        pivot_longer(cols = -group, names_to = "training", values_to = "mean") %>% 
        mutate(training = as.numeric(gsub("[^0-9]","",training))) %>% 
        ggplot(aes(x=training, y = mean, group = group, color = group)) + 
        geom_line()
    

    enter image description here