R均值按组排序用ggplot绘制线图

R mean sorted by groups plot line diagram with ggplot

我有一些这样的数据。最后会有20次训练。

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

我想用每个训练的均值绘制一个折线图,按组分类。

我希望有人能帮助我,我是 R 的新手。

有很多方法可以做到这一点,但如果不尝试 fiddle 一两分钟我就不能肯定地说。这里有人可以,但通常更喜欢与来自 op 的小的可重现 df。您可以使用 dplyr/tidyverse 中的 mutate() 命令,或者您可以使用带有单独函数的 apply() 函数,如下所示:

df<-apply(your_data, 2, function(x) if (your_data[,1]=="Group1") {mean} else {mean}) 但是我不知道它是否可以在不先测试的情况下工作。您还可以使用 which() 命令或 ifelse() ,如下所示:

df<-ifelse(your_data[,1]=="Group1", mean(yourdata[,2:length(your_data)]), mean(yourdata[,2:length(your_data)]))

但还是不知道在没有 trying/fiddling

的情况下是否有效

你的意思是这样的吗?

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()

Test
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() +
scale_colour_manual(values = c("lightskyblue", "royalblue"), name = "Legende") +
ggtitle("Persönliches Intensitäslevel pro Training sortiert nach Gruppen") +
theme_bw() +
theme(axis.text.x = element_text(size = 12)) +
theme(axis.title.y = element_text(size = 15, angle = 90)) +
theme(axis.text.y = element_text(size = 12, hjust=1)) +
theme(axis.title.x = element_text(size = 15)) +
scale_y_continuous(name = "Intensität", breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) +
theme(plot.title = element_text(hjust = 0.5))

现在我有了这个漂亮的线图。 我想更改 x 任何 y 轴标题和 x 轴上的中断。