使用 geom_point 在 R 中绘制多列和分组 [关闭]

Posted

技术标签:

【中文标题】使用 geom_point 在 R 中绘制多列和分组 [关闭]【英文标题】:plotting multiple columns and grouping in R with geom_point [closed] 【发布时间】:2022-01-17 14:15:52 【问题描述】:

嗨,我有如下数据:

总共 38 列。治疗栏中的 10 种治疗类型,日期栏中的日期为 25-29 数据示例代码:

df <- structure(
    list(
      Christensenellaceae = c(
        0,
        0.009910731,
        0.010131195,
        0.009679938,
        0.01147601,
        0.010484508,
        0.008641566,
        0.010017172,
        0.010741488,
        0.1,
        0.2,
        0.3,
        0.4),
    date=c(25,25,25,25,25,27,27,27,27,27,27,27,27),
      
      Treatment = c(
        "Original Sample_25",
        "Original Sample_25",
        "Original Sample_25",
        "Original Sample_25",
        "Original Sample_25",
        "Treatment 1_27",
        "Treatment 1_27",
        "Treatment 1_27",
        "Treatment 1_27",
        "Treatment 2_27",
        "Treatment 2_27",
        "Treatment 2_27",
        "Treatment 2_27")
    ),class = "data.frame",
    row.names = c(NA,-9L)
  )
    

我想要做的是为每一列创建 2 个图,一个用于原始样本,一个用于治疗(日期和治疗列除外),并且治疗列将有 10 条平均线作为治疗次数(见数据图片)我只为治疗 1 做到了这一点:看起来像这样:

但遗憾的是不知道如何按治疗类型对平均线进行分组。 这是我的代码:

data_work %>% 
  pivot_longer(-treatment) %>% 
  mutate(plot = ifelse(str_detect(treatment, "original"), 
                       "Original sample", 
                       "Treatment 1"),
         treatment = str_extract(treatment, "\\d+$")) %>% 
  group_by(name) %>% 
  group_split() %>% 
  map(~.x %>% ggplot(aes(x = factor(treatment), y = value, color = factor(name))) +
        geom_point() +
        stat_summary(aes(y = value,group=1), fun.y=mean, colour="red", geom="line",group=1)
        +
        facet_wrap(~plot, scales = "free_x") +
        labs(x = "Treatment", y = "Value", color = "Taxa") +
        guides(x =  guide_axis(angle = 90))+
        theme_bw())

谢谢你:)

当我对所有数据运行上面的代码时,我得到了这个:

但是根据治疗类型(1-10),我应该有 10 条线,而不是一条平均线

我考虑过计算每种治疗类型的平均值并将其添加到绘图代码中,但不知道如何添加它:

mean_1=df_3 %>% 
  group_by(treatment) %>%
  summarise(across(everything(), mean))

【问题讨论】:

您可以尝试计算图外的平均值并将其添加到数据中。然后你可以按照上面的答案here 做一个hline per facet @Jonny Phelps 添加了按治疗类型计算方法的代码,但我不明白如何从您的链接将其添加到绘图中 【参考方案1】:

您可以从df 中提取类型(原始或处理)和处理编号,并将它们用作ggplot2 中的美学,分别用于分面和分组着色。

library(tidyverse)
df <- list(
  Christensenellaceae = c(
    0,
    0.009910731,
    0.010131195,
    0.009679938,
    0.01147601,
    0.010484508,
    0.008641566,
    0.010017172,
    0.010741488,
    0.1,
    0.2,
    0.3,
    0.4
  ),
  Enterobacteriaceae = c(
    0,
    0.009910731,
    0.010131195,
    0.009679938,
    0.01147601,
    0.010484508,
    0.008641566,
    0.010017172,
    0.010741488,
    0.1,
    0.2,
    0.3,
    0.4
  ),
  Treatment = c(
    "Original Sample1",
    "Original Sample2",
    "Original Sample3",
    "Original Sample4",
    "Original Sample5",
    "Treatment 1",
    "Treatment 1",
    "Treatment 1",
    "Treatment 1",
    "Treatment 2",
    "Treatment 2",
    "Treatment 2",
    "Treatment 2"
  ),
  Date = c(25, 25, 25, 25, 25, 27, 27, 27, 27, 27, 27, 27, 27)
) %>% as_tibble()

df %>%
  pivot_longer(-c(Treatment, Date), names_to = "taxon") %>%
  mutate(
    type = Treatment %>% str_detect("Original") %>% ifelse("Original", "Treatment"),
    treatment_nr = Treatment %>% str_extract("(?<=Treatment )[0-9]+")
  ) %>%
  ggplot(aes(Date, value, color = treatment_nr)) +
  geom_point() +
  stat_summary(
    geom = "point",
    fun.y = "mean",
    size = 3,
    shape = 24
  ) +
  geom_line() +
  facet_grid(type ~ taxon, scales = "free_y")
#> Warning: `fun.y` is deprecated. Use `fun` instead.

由reprex package (v2.0.1) 于 2021 年 12 月 14 日创建

【讨论】:

这似乎是针对 1 列“Christensenellaceae”是否适用于数据中的所有列(37),除了治疗? 不,因为我只能使用您的示例数据。但是您可以在mutate 之前使用pivot_longer 将所有37 个科融合为一个分类单元和一个值列 试过了,没用 :df_3 %>% pivot_longer(-treatment) %>% mutate(plot = ifelse(str_detect(treatment, "Original"), "Original sample", "Treatment") , 治疗 = str_extract(治疗, "\\d+$")) %>% group_by(name) %>%group_split() %>% map(~.x %>% ggplot(aes(x = factor(treatment), y = value, color = factor(name))) + geom_point() +stat_summary(aes(y = value,group=1), fun.y=mean, colour="red", geom="line",group= 1) + facet_wrap(~plot, scales = "free_x") + labs(x = "Treatment", y = "Value", color = "Taxa") +guides(x = guide_axis(angle = 90))+theme_bw( )) @ElizaR 我修改了答案并添加了平均标记和多个分类群 df_3 %>% pivot_longer(-treatment, names_to = "taxon") %>% mutate( type = treatment %>% str_detect("Original") %>% ifelse("Original", "治疗"),treatment_nr = 治疗 %>% str_extract("(?%ggplot(aes(value, color = treatment_nr)) +geom_point() +stat_summary(geom = "point",fun.y = "mean", size = 3, shape = 24) +geom_line() +facet_grid(type ~ taxon, scales = "free_y")-错误:stat_summary 需要以下缺失的美学:yRun @ 987654328@ 查看错误发生的位置。此外:警告消息:fun.y 已弃用。请改用fun

以上是关于使用 geom_point 在 R 中绘制多列和分组 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 R 中使用 ggplot 绘制多列? [复制]

R/graphics:在函数中绘制为 PNG

在R中用ggplot和par绘制两个图

两个geom_points添加一个图例

如何在 R 中使用 ggplot2 绘制的绘图的 y 轴刻度中准确显示数字的 SI 前缀?

【R语言】--- 散点图