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

调整ggplot2分组箱线图:移除重复图例及优化箱线显示

解决方案

1. 移除重复图例

由于你同时对fill和colour映射了同一个变量variable,ggplot会自动生成两个重复图例。只需在scale_fill_manual中添加guide = FALSE,关闭填充对应的图例,只保留颜色图例即可。

2. 调整箱线图样式(避免须线穿透+清晰中位数)

  • 图层顺序调整:将geom_boxplot放在stat_boxplot之后,让箱体图层覆盖须线的穿透部分;
  • 中位数强化:在geom_boxplot中添加median.colour = "black"参数,让中位数线与箱体颜色形成对比,更清晰可见;
  • 完整保留你原有的颜色设置与其他样式参数。

修改后的完整代码

library(ggplot2)
library(reshape2)

## 创建示例数据
createDF <- quote(data.frame(id=sample(c("NN", "SS","H"), 100, rep=T),
                             heavy=runif(100),
                             heavier=runif(100),
                             heaviest=runif(100)))
dfs <- lapply(1:4, function(i) eval(createDF))

## 数据合并与重塑
dat <- do.call(rbind, dfs)
dat$dfid <- paste("df", rep(1:4, times=sapply(dfs, nrow)))
dat <- melt(dat, id.vars=c("id", "dfid"))

# 绘制优化后的分组箱线图
ggplot(dat, aes(x = factor(id, level = c('H', 'NN', 'SS')), # 修正原x轴level错误(原NS不存在)
                y=value, 
                group=interaction(variable, id), 
                fill=variable, 
                colour=variable)) +
  # 先绘制须线(errorbar)
  stat_boxplot(geom ='errorbar', width = 0.6,lwd = 0.9,
               position = position_dodge2(preserve = "single"))+
  # 后绘制箱体,覆盖须线穿透部分,同时设置中位数颜色
  geom_boxplot(width = 0.6,lwd = 0.9,outlier.shape = NA,
               outlier.colour = NULL, coef = 0, 
               position = position_dodge2(preserve = "single"),
               alpha = 0.6,fatten = 2,
               median.colour = "black") + # 新增中位数颜色参数
  coord_cartesian(ylim = c(0, 1))+
  scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, by = .25),expand = c(0, 0)) + 
  facet_grid(.~dfid)+
  # 关闭fill的图例,移除重复项
  scale_fill_manual(values=c("#4daf4a","#984ea3","#ffff99","#ff7f00","#e41a1c"),
                    name="", guide = FALSE)+ 
  scale_color_manual(name = "scenario", 
                     values = c("#4daf4a","#984ea3","#ffff99","#ff7f00","#e41a1c"))+
  labs(y = "Values", x = "")+
  theme_bw()+
  theme(strip.text.x = element_text(size = 14, colour = "black", angle = 0, face = "bold"),
        axis.text = element_text(size = 12, color = "black"),
        text = element_text(size = 18),
        axis.text.x = element_text(angle = 0, hjust = 0.5, size = 9, colour = "black", face = "bold"),
        legend.direction = "vertical",
        legend.position = c(0.5, .79),
        panel.background = element_rect(fill = "grey70"),
        panel.border = element_rect(colour = "black", fill=NA, size=1),
        legend.title = element_blank(),
        legend.text = element_text(colour="black", size = 10, face = "bold"),
        legend.margin=margin(-10, 0, 0, 0),
        panel.grid.major = element_line(colour="white", size = 0.2),
        panel.grid.minor = element_blank())

额外修正说明

原代码中x轴的level设置包含'NS',但数据中不存在该类别,已修正为c('H', 'NN', 'SS'),避免出现空的x轴标签。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 03:01:40