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

如何修改dabestr包dabest_plot函数调色板及导出数据至ggplot2?

解决方案

一、修复dabestr自定义调色板无效问题

dabestr的dabest_plot函数中,自定义离散调色板的正确参数是color_palette,而非你尝试的palette或custom_palette。直接传入颜色向量即可生效:

方法1:通用颜色向量

如果不需要指定对应分组的颜色,直接传入颜色列表即可,dabestr会自动为分组分配颜色:

# 自定义颜色向量
custom_colors <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
dabest_plot(multi_groups.mean_diff, color_palette = custom_colors)

方法2:指定分组对应颜色

如果需要为每个分组精准匹配颜色,可以命名颜色向量:

# 为你的分组指定对应颜色
group_colors <- c(
  "GB-L" = "#FF0000", "LDPE-L" = "#00FF00", 
  "GB-H" = "#0000FF", "LDPE-H" = "#FFFF00", 
  "GF-L" = "#FF00FF", "PA-L" = "#00FFFF", 
  "GF-H" = "#880000", "PA-H" = "#008800"
)
dabest_plot(multi_groups.mean_diff, color_palette = group_colors)

二、提取dabestr数据并用ggplot2绘制

如果希望改用ggplot2完全自定义绘图,可以从dabestr的分析对象中提取所需数据:

1. 提取原始数据

dabestr分析对象的$data属性包含用于分析的原始数据集:

# 提取原始数据
raw_data <- multi_groups.mean_diff$data

2. 提取均值差统计数据

$mean_diff属性包含计算得到的均值差、BCA置信区间等统计结果:

# 提取均值差统计数据
diff_stats <- multi_groups.mean_diff$mean_diff

3. 用ggplot2复刻dabestr风格可视化

以下代码实现类似dabestr的分组散点+分布展示,同时添加均值差的对比:

library(ggplot2)
library(patchwork) # 用于合并两个图

# 绘制原始数据的分组图
group_plot <- ggplot(raw_data, aes(x = Treatment, y = Total_CO2, color = Treatment)) +
  geom_jitter(width = 0.2, alpha = 0.6, size = 2) + # 抖动散点避免重叠
  geom_violin(fill = NA, linewidth = 1) + # 小提琴图展示数据分布
  scale_color_manual(values = group_colors) + # 应用自定义调色板
  labs(y = "Total CO₂") +
  theme_minimal() +
  theme(legend.position = "none", axis.title.x = element_blank())

# 绘制均值差对比图
diff_plot <- ggplot(diff_stats, aes(x = paste(group1, "vs", group2), y = difference)) +
  geom_point(size = 3, color = "#2c3e50") +
  geom_errorbar(aes(ymin = bca_low, ymax = bca_high), width = 0.2, color = "#2c3e50") +
  labs(x = "", y = "Mean Difference") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# 合并两个图
group_plot + diff_plot + plot_layout(widths = c(3, 2))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 02:06:08