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

如何修改R语言ggplot箱线图图例填充色以显示条纹图案

解决方案:修改condition图例的填充底色以显示白色条纹

要解决图例中白色条纹不可见的问题,只需在scale_pattern_manual的guide_legend(override.aes)中额外添加fill参数,把图例的填充底色设置为黑色或灰色,就能让白色条纹清晰显示。

修改后的代码

## Loading of packages
library(ggplot2)
library(readxl)
library(ggpattern)

##data input
data <- read_excel("DATA_V1_VTM_23_24.xlsx")

##boxplot formation
plot_1 <- ggplot(data=data, aes(x = tp,y = Fv_Fm, fill = genotype, group = interaction(tp, genotype, condition)))+
  geom_boxplot_pattern(aes(pattern = condition, pattern_fill = condition), 
                       pattern_density = 0.25, outlier.shape = NA)+
  scale_pattern_manual(values= c("c"="none","s"= "stripe"), 
                       guide = guide_legend(override.aes = list(
                         pattern = c("c"="none", "s"="stripe"), 
                         pattern_fill = c("c"="black","s"="white"),
                         fill = c("black", "black")  # 设置图例填充底色为黑色
                       )))+
  scale_pattern_fill_manual(values=c("c"="black","s"="white"))

##show boxplot
plot_1

关键修改说明

  • 在guide_legend(override.aes)中加入fill = c("black", "black"),强制把condition图例的两个条目填充色都设为黑色,这样"s"组的白色条纹就会在黑色背景上清晰显现,"c"组无图案也能正常显示。
  • 如果偏好灰色背景,可以把black换成gray50这类灰度值。

原问题回顾

使用ggplot2和ggpattern绘制箱线图时,为condition="s"的组别设置了白色条纹,但图例中箱线图的填充色与条纹色一致(白色),导致条纹无法识别,需要修改图例填充底色来解决。

原代码

## Loading of packages
library(ggplot2)
library(readxl)
library(ggpattern)

##data input
data <- read_excel("DATA_V1_VTM_23_24.xlsx")

##boxplot formation
plot_1 <- ggplot(data=data, aes(x = tp,y = Fv_Fm, fill = genotype, group = interaction(tp, genotype, condition)))+geom_boxplot_pattern(aes(pattern = condition, pattern_fill = condition), pattern_density = 0.25, outlier.shape = NA)+scale_pattern_manual(values= c("c"="none","s"= "stripe"), guide = guide_legend(override.aes = list(pattern = c("c"="none", "s"="stripe"), pattern_fill = c("c"="black","s"="white"))))+scale_pattern_fill_manual(values=c("c"="black","s"="white"))

##show boxplot
plot_1

生成的图像

箱线图

数据集

group;genotype;condition;tp;rep No;Fv_Fm;Y_II;Y_NO;Y_NPQ
1;Col-0;c;15;1;0.811;0.564;0.342;0.094
1;Col-0;c;15;2;0.799;0.494;0.312;0.194
1;Col-0;c;15;3;0.74;0.326;0.325;0.349
1;Col-0;c;15;4;0.812;0.422;0.328;0.25
2;Col-0;s;15;1;0.822;0.447;0.3;0.253
2;Col-0;s;15;2;0.811;0.38;0.308;0.312
2;Col-0;s;15;3;0.803;0.319;0.352;0.329
2;Col-0;s;15;4;0.81;0.112;;
3;inv4;c;15;1;0.814;0.317;0.161;0.522
3;inv4;c;15;2;0.74;0.33;0.163;0.507
3;inv4;c;15;3;0.803;0.31;0.303;0.387
3;inv4;c;15;4;0.812;0.328;0.25;0.422
4;inv4;s;15;1;0.817;0.658;0.202;0.14
4;inv4;s;15;2;0.746;0.286;0.363;0.351
4;inv4;s;15;3;0.803;0.232;0.358;0.41
4;inv4;s;15;4;0.822;0.822;;
5;Col-0;c;30;1;0.793;0.485;0.323;0.192
5;Col-0;c;30;2;0.759;0.336;0.355;0.309
5;Col-0;c;30;3;0.793;0.460;0.321;0.219
5;Col-0;c;30;4;0.805;0.178;0.439;0.383
1;Col-0;s;30;1;0.801;0.268;0.3;0.253
1;Col-0;s;30;2;0.793;0.238;0.492;0.27
1;Col-0;s;30;3;0.788;0.222;0.43;0.348
1;Col-0;s;30;4;0.822;0.136;;
2;inv4;c;30;1;0.769;0.49;0.319;0.191
2;inv4;c;30;2;0.815;0.512;0.321;0.167
2;inv4;c;30;3;0.79;0.351;0.329;0.32
2;inv4;c;30;4;0.742;0.112;;
3;inv4;s;30;1;0.726;0.285;0.478;0.237
3;inv4;s;30;2;0.753;0.426;0.29;0.284
3;inv4;s;30;3;0.817;0.194;0.439;0.367
3;inv4;s;30;4;0.755;0.196;0.397;0.407
4;Col-0;c;45;1;0.793;0.512;0.316;0.172
4;Col-0;c;45;2;0.766;0.472;0.356;0.172
4;Col-0;c;45;3;0.782;0.469;0.31;0.221
4;Col-0;c;45;4;0.789;0.47;;
5;Col-0;s;45;1;0.74;0.035;0.971;-0.006
5;Col-0;s;45;2;0.169;0.020;0.978;0.002
5;Col-0;s;45;3;0.799;0.082;0.684;0.234
5;Col-0;s;45;4;0.814;0.115;0.504;0.381
4;inv4;c;45;1;0.798;0.331;0.21;0.459
4;inv4;c;45;2;0.754;0.6;0.243;0.157
4;inv4;c;45;3;0.76;0.329;0.265;0.406
4;inv4;c;45;4;0.763;0.336;0.361;0.303
1;inv4;s;45;1;0.472;0.025;0.953;0.022
1;inv4;s;45;2;0.785;0.069;0.826;0.105
1;inv4;s;45;3;0.797;0.101;0.699;0.2
1;inv4;s;45;4;0.801;0.801;;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 06:27:03