繁体   English   中英

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 的新手。

你的意思是这样的吗?

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

在此处输入图片说明

有办法做到这一点,但我不能肯定地说不尝试摆弄一两分钟。这里有人可以,但通常更喜欢使用来自 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)]))

但同样不知道它是否可以在不尝试/摆弄的情况下工作

测试
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 轴上的中断。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM