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

如何绘制含测量时间、实验组/对照组及特定班级的分组条形图?

实现融合实验组、对照组与特定班级的条形图方案

1. 预处理数据:计算所需均值

首先生成包含实验组、对照组、目标班级三类分组的统计数据集,以选定5050作为特定班级为例:

library(dplyr)
library(ggplot2)

# 指定要展示的特定班级
target_class <- "5050"

# 计算三类分组的均值
summary_data <- textscores_long %>%
  # 先统计实验组、对照组的时间点均值
  group_by(MZP, ExCon) %>%
  summarise(Textscores = mean(Textscores), .groups = "drop") %>%
  mutate(group = case_when(
    ExCon == "1" ~ "实验组",
    ExCon == "2" ~ "对照组"
  )) %>%
  select(-ExCon) %>%
  # 追加特定班级的时间点均值
  bind_rows(
    textscores_long %>%
      filter(class == target_class) %>%
      group_by(MZP) %>%
      summarise(Textscores = mean(Textscores), .groups = "drop") %>%
      mutate(group = paste0("班级", target_class))
  )

2. 绘制分组条形图

使用ggplot2绘制x轴为测量时间、每组时间点下包含三类并排条形的可视化图:

ggplot(summary_data, aes(x = MZP, y = Textscores, fill = group)) +
  # 绘制并排条形
  geom_col(position = position_dodge(width = 0.8), width = 0.7) +
  # 添加均值数值标签
  geom_text(aes(label = round(Textscores, 1)), 
            position = position_dodge(width = 0.8), 
            vjust = -0.5, size = 3.5) +
  # 设置轴标签和图例标题
  labs(x = "测量时间", y = "文本分数均值", fill = "分组") +
  # 使用简洁主题
  theme_minimal() +
  # 调整图例位置到顶部
  theme(legend.position = "top")

自定义调整说明

  • 更换特定班级:修改target_class变量的取值即可
  • 调整条形间距:修改position_dodge和geom_col中的width参数
  • 自定义颜色:添加scale_fill_manual(values = c("实验组" = "#2E86AB", "对照组" = "#F24C4C", "班级5050" = "#F7DC6F"))来指定各分组颜色

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 04:42:44