如何让ggplot2使用指定颜色绘制浮游生物叶绿素a箱线图
问题:指定区域颜色的带图案箱线图不生效
我有墨西哥湾5个区域两类浮游生物('WSW'和'<20 µm')的叶绿素a生物量数据,每个区域都包含两类数据。想要绘制箱线图实现以下效果:
- 为每个区域指定特定颜色(需与其他图表统一)
- 为其中一类尺寸添加图案区分两类数据
目前图案已成功添加,但ggplot2始终使用默认颜色,无法应用我指定的region_colors颜色。以下是我原代码及数据:
region_colors <- c('WFS' = 'darkgreen', 'TLS' = 'cyan3', 'EMS' = 'orangered2', 'CB' = 'yellow3', 'YC/FS' = 'plum2') surf_boxplot= ggplot(surf_box, aes(x = region, y = Chla, fill = region, pattern = Size)) + geom_boxplot_pattern(pattern_fill = "white", pattern_color = "white", pattern_angle = 45, pattern_size = 0, pattern_spacing = 0.025) + scale_pattern_fill_manual(values = region_colors) + scale_pattern_manual(values = patterns) + theme(panel.background = element_blank(), legend.position = 'top', axis.line.x = element_line(color="black", size = 1), axis.line.y = element_line(color="black", size = 1), plot.title = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0), hjust = 0.5, size = 15, face = 'bold'), legend.key = element_blank(), legend.title = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0), size = 10, face = "bold")) + labs(x=expression(bold(paste("Regions of the Gulf of Mexico"))), y=expression(bold(paste("Chl-",bolditalic ("a"), " biomass (µg L"^'-1',')'))), title = expression(bold(paste("Surface Chl-", bolditalic ("a"), ' Biomass'))))
数据:
structure(list(Size = c("WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "WSW", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm", "<20 µm"), Chla = c(0.0158553374250702, 0.0681835535205316, 1.69758559710116, 0.38623655064116, 0.18999889055035, 0.0475280806809533, 0.0518441820414833, 1.38653044994664, 0.811092911431239, 0.0627435113039509, 0.0600206049988672, 0.76576153727216, 0.0131834301368552, 0.0139065718743198, 0.0801018539960821, 0.0476673877342761, 0.0170883088104008, 0.0353610062847066, 0.0862740442259294, 0.54829425475163, 0.354272113611421, 0.0419050024352668, 0.14044768258058, 0.0693766746908316, 0.0539223064788303, 0.0159113633523673, 0.033895686014726, 0.592754310802978, 0.251252264220314, 0.177521351469432, 0.0574533958589567, 0.0447197172789116, 1.75919476060424, 0.789171481392557, 0.104392221393643, 0.0613148498505948, 0.646043888487357, 0.0200254634990205, 0.0165766336741892, 0.0619120579844718, 0.0324340598144779, 0.0264756363476934, 0.0276985089356931, 0.105004593301296, 0.288904529677619, 0.295226761342851, 0.0419623280199936, 0.10089302911911, 0.0580098748827292, 0.0647851594974109 ), region = c("WFS", "WFS", "WFS", "WFS", "WFS", "WFS", "TLS", "TLS", "TLS", "TLS", "TLS", "TLS", "EMS", "EMS", "EMS", "EMS", "EMS", "CB", "CB", "CB", "CB", "CB", "CB", "YC/FS", "YC/FS", "WFS", "WFS", "WFS", "WFS", "WFS", "WFS", "TLS", "TLS", "TLS", "TLS", "TLS", "TLS", "EMS", "EMS", "EMS", "EMS", "EMS", "CB", "CB", "CB", "CB", "CB", "CB", "YC/FS", "YC/FS"), depth_name = c("Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface", "Surface"), distance = c("Offshore", "Offshore", "Nearshore", "Nearshore", "Intermediate", "Offshore", "Offshore", "Nearshore", "Nearshore", "Intermediate", "Intermediate", "Nearshore", "Nearshore", "Intermediate", "Offshore", "Intermediate", "Offshore", "Offshore", "Intermediate", "Nearshore", "Nearshore", "Offshore", "Intermediate", "Offshore", "Intermediate", "Offshore", "Offshore", "Nearshore", "Nearshore", "Intermediate", "Offshore", "Offshore", "Nearshore", "Nearshore", "Intermediate", "Intermediate", "Nearshore", "Nearshore", "Intermediate", "Offshore", "Intermediate", "Offshore", "Offshore", "Intermediate", "Nearshore", "Nearshore", "Offshore", "Intermediate", "Offshore", "Intermediate")), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame"))
解决方案
问题核心在于你错误地使用了scale_pattern_fill_manual来设置箱线图的主体填充色,实际上fill美学映射的是region,应该用scale_fill_manual来指定区域颜色。同时补充patterns变量的定义,确保图案映射生效。
修正后的代码:
# 定义区域颜色 region_colors <- c('WFS' = 'darkgreen', 'TLS' = 'cyan3', 'EMS' = 'orangered2', 'CB' = 'yellow3', 'YC/FS' = 'plum2') # 定义图案:给<20 µm添加条纹,WSW无图案 patterns <- c('WSW' = '', '<20 µm' = 'stripe') surf_boxplot <- ggplot(surf_box, aes(x = region, y = Chla, fill = region, pattern = Size)) + geom_boxplot_pattern( pattern_fill = "white", # 图案内部填充色 pattern_color = "white", # 图案线条颜色 pattern_angle = 45, # 图案角度 pattern_size = 0.2, # 图案线条粗细(原0可能导致图案不显示) pattern_spacing = 0.025 # 图案间距 ) + scale_fill_manual(values = region_colors) + # 关键:用这个设置fill=region的颜色 scale_pattern_manual(values = patterns) + theme( panel.background = element_blank(), legend.position = 'top', axis.line.x = element_line(color="black", size = 1), axis.line.y = element_line(color="black", size = 1), plot.title = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0), hjust = 0.5, size = 15, face = 'bold'), legend.key = element_blank(), legend.title = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0), size = 10, face = "bold") ) + labs( x=expression(bold(paste("Regions of the Gulf of Mexico"))), y=expression(bold(paste("Chl-",bolditalic ("a"), " biomass (µg L"^'-1',')'))), title = expression(bold(paste("Surface Chl-", bolditalic ("a"), ' Biomass'))) ) # 查看图形 print(surf_boxplot)
关键修正说明:
- 替换
scale_pattern_fill_manual为scale_fill_manual(values = region_colors):这是控制fill = region映射颜色的正确scale,直接作用于箱线图的主体填充。 - 补充
patterns变量定义:明确两类尺寸对应的图案类型,确保图案映射生效。 - 调整
pattern_size从0到0.2:原0值会导致图案线条不可见,适当调整后条纹图案能正常显示。
内容的提问来源于stack exchange,提问作者garfunkl_30
相关产品推荐
相关产品推荐

