You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R中geom_point为数据框多列分别绘制两组处理散点图

R数据框批量绘制分组散点图实现方案

我现有一个共36列的R数据框,数据结构如下图所示:
数据结构示例

数据样例构造代码如下:

df <-
  structure(
    list(
      Bacteroidaceae = c(
        0,
        0.10944999,
        0.104713314,
        0.125727668,
        0.124136247,
        0.005155911,
        0.005072778,
        0.010231826,
        0.010188139
      ),
      Christensenellaceae = c(
        0,
        0.009910731,
        0.010131195,
        0.009679938,
        0.01147601,
        0.010484508,
        0.008641566,
        0.010017172,
        0.010741488
      ),
      treatment = c(
        "Original Sample1",
        "Original Sample2",
        "Original Sample3",
        "Original Sample4",
        "treatment1_1",
        "treatment1_2",
        "treatment1_3",
        "treatment1_4"
      )
    ),
    class = "data.frame",
    row.names = c(NA,-8L)
  )

我需要为除treatment列之外的每列分别绘制2张散点图:1张对应treatment1组、1张对应Original样本组,总计生成72张图,参考效果如下图所示:
参考效果图

我尝试运行了以下代码实现该需求:

df %>%
  tidyr::pivot_longer(!treatment, names_to = "taxa", values_to = "value") %>%
  dplyr::filter(str_detect(treatment, "Treatment1")) %>%
   for (i in columns(df)){
    )
  ggplot(aes(x = treatment, y = value, color = taxa),group=treatment) +
  geom_point() +
  stat_summary(fun.y = mean,
               geom = "line", width = 0.5)+geom_jitter(widh=0.25)
  theme_bw()}}

但代码未能正常运行,以下是可行实现方案:


原代码错误原因

  1. 管道符后不能直接接for循环,逻辑链路断裂
  2. 存在多处拼写错误:geom_jitter的参数是width而非widh,str_detect匹配的Treatment1首字母大写和实际数据里小写开头的treatment1不匹配,导致筛选不到数据
  3. 仅筛选了treatment1组,未单独处理Original组
  4. 循环逻辑错误,没有按物种拆分单独出图的逻辑

实现代码

首先加载依赖包:

library(tidyverse)

批量绘图代码:

# 长表转换,新增大分组标签
df_long <- df %>%
  pivot_longer(!treatment, names_to = "taxa", values_to = "value") %>%
  mutate(group = case_when(
    str_detect(treatment, "Original") ~ "Original",
    str_detect(treatment, "treatment1") ~ "treatment1"
  )) %>%
  drop_na(group)

# 提取所有物种列名和两个大分组
taxa_list <- setdiff(colnames(df), "treatment")
group_list <- c("Original", "treatment1")

# 批量绘图并保存
walk(taxa_list, function(current_taxa) {
  walk(group_list, function(current_group) {
    # 筛选对应物种和分组的绘图数据
    plot_data <- filter(df_long, taxa == current_taxa, group == current_group)
    
    # 生成散点图
    p <- ggplot(plot_data, aes(x = treatment, y = value)) +
      geom_jitter(width = 0.25, size = 2, color = "#2c3e50") +
      stat_summary(fun = mean, geom = "line", color = "#e74c3c", linewidth = 0.8, group = 1) +
      labs(title = paste0(current_group, "_", current_taxa), x = "处理", y = "丰度") +
      theme_bw() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
    
    # 保存图片到工作目录,可自行修改路径、尺寸、分辨率参数
    ggsave(paste0(current_group, "_", current_taxa, ".png"), p, width = 6, height = 4, dpi = 300)
  })
})

代码运行后会自动在你的R工作目录生成按分组_物种名规则命名的72张散点图。如果需要将图存储在列表中后续调用,把walk替换为map,去掉ggsave行,改为return(p)即可。


内容的提问来源于stack exchange,提问作者Eliza R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 15:36:04