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

如何为每个Class.1生成独立统计图,且图内按Condition分组?

按Class.1分组生成RULA指标统计图方案

前置准备

先加载所需的R包:

library(tidyverse)

数据格式转换

将宽格式数据转为长格式,方便ggplot处理多指标:

captiv_long <- Captiv_mean %>%
  select(-Participant.Code) %>%  # 忽略Participant.Code列
  pivot_longer(
    cols = ends_with("RULA"),  # 选中所有以RULA结尾的列
    names_to = "RULA_指标", 
    values_to = "均值得分"
  )

方案1:生成包含所有Class的分面图

一次性生成4个Class的子图,放在同一画布中:

ggplot(captiv_long, aes(x = RULA_指标, y = 均值得分, fill = Condition)) +
  geom_col(position = position_dodge(width = 0.8)) +  # 分组柱状图,避免重叠
  facet_wrap(~Class.1, scales = "free_y") +  # 每个Class.1对应一个子图,y轴自适应
  labs(
    title = "不同Class与Condition下的RULA均值得分",
    x = "RULA身体部位指标",
    y = "均值得分",
    fill = "Condition"
  ) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  # 旋转x轴标签,避免重叠

方案2:生成独立的单Class统计图并保存

如果需要每个Class单独生成一张图并保存为文件:

# 按Class.1拆分数据并批量绘图
class_plots <- captiv_long %>%
  group_split(Class.1) %>%
  map(function(df) {
    ggplot(df, aes(x = RULA_指标, y = 均值得分, fill = Condition)) +
      geom_col(position = position_dodge(width = 0.8)) +
      labs(
        title = paste("RULA均值得分 -", unique(df$Class.1)),
        x = "RULA身体部位指标",
        y = "均值得分",
        fill = "Condition"
      ) +
      theme_bw() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
  })

# 将每个图保存为PNG文件
walk2(
  class_plots, 
  unique(Captiv_mean$Class.1), 
  function(plot, class_name) {
    ggsave(
      paste0("RULA得分_", class_name, ".png"), 
      plot, 
      width = 8, 
      height = 6, 
      dpi = 300
    )
  }
)

说明

  • 上述代码默认使用柱状图展示均值,如果你需要其他类型的图(如点图、折线图),可以将geom_col替换为geom_point或geom_line
  • 如果需要添加误差棒,需要补充原始数据的标准差/标准误信息,可告知补充数据后调整代码

内容的提问来源于stack exchange,提问作者Richard Michaud Langis

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 17:31:10