如何为Y轴含分类变量的ggplot2点须图添加交替灰白背景
解决方案:为ggplot2横向点须图添加物种交替背景条
要实现每个物种区域的交替背景条,核心是用geom_rect在图层最底层绘制横跨X轴的矩形,结合分类变量的整数位置定位y轴范围。以下是修改后的完整代码:
library(tidyverse) tr_df <- structure(list(species = c("American Black Duck", "American Wigeon", "Blue-winged Teal", "Canada Goose", "Green-winged Teal", "Mallard", "Northern Shoveler", "Pied-billed Grebe", "Red-winged Blackbird", "Ring-necked Duck", "Sora", "Swamp Sparrow", "Wood Duck", "American Black Duck", "American Wigeon", "Blue-winged Teal", "Canada Goose", "Green-winged Teal", "Mallard", "Northern Shoveler", "Pied-billed Grebe", "Red-winged Blackbird", "Ring-necked Duck", "Sora", "Swamp Sparrow", "Wood Duck"), parameter = c("dd", "dd", "dd", "dd", "dd", "dd", "dd", "dd", "dd", "dd", "dd", "dd", "dd", "pt", "pt", "pt", "pt", "pt", "pt", "pt", "pt", "pt", "pt", "pt", "pt", "pt"), effect_size = c(-0.41, -0.69, 1.16, -0.4, 1.75, -0.58, -0.04, -5.95, -0.21, -2.74, -4.26, 0.17, -2.83, 0.24, -0.21, 0.57, 0.25, 0.2, -0.53, 0.15, -1.77, 0.31, -0.84, 0.26, 0.39, -0.08), LCI = c(-1.1352, -1.5328, 0.1604, -1.2624, 0.8092, -1.462, -0.5496, -8.204, -0.7196, -3.7984, -6.5924, -0.2808, -4.2412, -0.5048, -1.0136, -0.4296, -0.6908, -0.7016, -1.3924, -0.34, -2.8676, -0.0624, -1.5652, -1.1512, -0.0216, -1.3344), UCI = c(0.3152, 0.1528, 2.1596, 0.4624, 2.6908, 0.302, 0.4696, -3.696, 0.2996, -1.6816, -1.9276, 0.6208, -1.4188, 0.9848, 0.5936, 1.5696, 1.1908, 1.1016, 0.3324, 0.64, -0.6724, 0.6824, -0.1148, 1.6712, 0.8016, 1.1744), shading = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0)), row.names = c(NA, -26L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame")) cols <- c("#fa5f2c", "#608cd9") tr_prot_whisker <- ggplot(tr_df, aes(effect_size, species, fill=parameter))+ # 添加交替背景矩形,放在最底层 geom_rect( data = distinct(tr_df, species), aes( xmin = -Inf, xmax = Inf, ymin = as.integer(factor(species)) - 0.5, ymax = as.integer(factor(species)) + 0.5, fill = as.factor(as.integer(factor(species)) %% 2) ), alpha = 0.3 ) + scale_fill_manual(values = c("white", "grey90"), guide = "none") + geom_vline(xintercept=0, linetype="dashed", color = "grey65", size=1.5) + geom_errorbarh(aes(xmin = LCI, xmax = UCI), color = "black", size = 1, height=0.8, position = position_dodge(width = 1)) + geom_point(color ="black",shape=21, stroke = 1, size=5, position = position_dodge(width = 1)) + scale_fill_manual(values = cols, guide = "none") + scale_x_continuous(limits=c(-10,5), breaks=c(-10,-8,-6,-4,-2,0,2,4))+ labs(y=NULL, x = expression("Standardized " * beta * " coefficients")) + theme_classic()+ theme(axis.line = element_line(colour="black", size=1.3), panel.border = element_blank(), legend.position = "none", axis.ticks.length = unit(0.2, "cm"), axis.title.x = element_text(size=24, colour="black"), axis.text.x = element_text(size=18, colour="black"), axis.title.y=element_text(size=24, colour="black", vjust=2), axis.text.y=element_text(size=16, colour="black")) tr_prot_whisker
关键修改说明
背景矩形图层:
- 用
distinct(tr_df, species)获取唯一物种列表,避免重复绘制矩形 xmin=-Inf和xmax=Inf让矩形横跨整个X轴范围as.integer(factor(species))将分类物种转换为对应y轴的整数位置,通过±0.5确定每个物种的垂直范围- 用
%%2实现奇偶行交替颜色,alpha=0.3降低背景透明度,避免覆盖前景元素
- 用
修正ggplot语法:
- 原代码中
aes(tr_df$effect_size, species)改为aes(effect_size, species),符合ggplot变量映射规范
- 原代码中
图层顺序:
geom_rect必须放在所有前景图层(点、误差线、竖线)之前,确保背景在最底层
内容的提问来源于stack exchange,提问作者Kiirsti Owen
相关产品推荐
相关产品推荐

