如何用ggpattern为双因素ANOVA分组柱状图添加Time因子图案
问题解决:为分组柱状图添加ggpattern图案区分Time因子
需求说明
绘制包含Type、Time两个因子,以及response a、b两个变量的分组柱状图,要求Time因子同时通过颜色和图案区分各组柱状图,目前仅实现颜色区分,未添加图案。
修改后代码
library(ggplot2) library(ggpubr) library(tidyverse) library(ggthemes) library(ggpattern) library(egg) library(multcompView) # 数据准备 example <- structure(list(Type = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), levels = c("A", "B", "C", "D", "E", "F"), class = "factor"), value = c(5, 5.3, 5.5, 2, 1.9, 3, 4, 4.5, 6, 8.1, 9, 10, 7, 7.5, 9, 12, 13.5, 16, 3.5, 3.8, 4, 0.5, 0.4, 1.5, 2.5, 3, 4.5, 6.6, 7.5, 8.5, 5.5, 6, 7.5, 10.5, 12, 14.5), response = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("a", "b"), class = "factor"), sd = c(0.895764470633308, 0.289362716931399, 0.104678169231026, 0.00930353935514927, 0.753359375020964, 0.638338206893093, 0.579651408510526, 0.964390131543651, 0.341300112732967, 0.76228717509891, 0.0845759617815788, 0.273269105643979, 0.857765282025983, 0.260972208876609, 0.805925034184099, 0.687981747137384, 0.989258579321479, 0.743281342584998, 0.168238567687899, 0.588839679785241, 0.0898999138186304, 0.0707352006427383, 0.808880144480776, 0.806555126114646, 0.0194885784995568, 0.800068228835156, 0.0935771768502066, 0.625519315733491, 0.946960588926455, 0.177769938144293, 0.532128866484254, 0.014955121437948, 0.03144820760672, 0.455614442351514, 0.975913745705029, 0.420061058948122), Time = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), levels = c("10", "20", "30"), class = "factor")), row.names = c(NA, -36L), class = c("tbl_df", "tbl", "data.frame")) # 确保因子类型正确 example$Type <- as.factor(example$Type) example$response <- as.factor(example$response) example$Time <- as.factor(example$Time) # 绘图代码(核心修改) plot <- ggplot(example, aes(x=Type, y=value, fill=Time, pattern=Time))+ # 替换为支持图案的柱状图几何对象 geom_col_pattern( position=position_dodge(width=0.8), color="black", width=0.8, pattern_fill="white", # 图案线条/点的颜色 pattern_density=0.15, # 图案密度 pattern_frequency=5 # 图案频率 )+ geom_errorbar(aes(ymax=value+sd, ymin=value-sd), position=position_dodge(0.8), width=0.25, color="black", alpha=0.5, show.legend = FALSE)+ # 颜色映射保持原设置 scale_fill_brewer(name="Time / min", palette = "Greens", guide = guide_legend(label.hjust = 0))+ # 手动指定各Time水平的图案类型 scale_pattern_manual( name="Time / min", values = c("10"="stripe", "20"="circle", "30"="crosshatch") )+ theme_bw(base_size = 20) + theme( panel.grid.major=element_blank(), panel.grid.minor=element_blank(), axis.text=element_text(size=20), axis.title=element_text(size=22,face="bold"), legend.title=element_text(size=14, face="bold"), legend.text=element_text(size=12), axis.text.x=element_text(angle = 45, hjust = 1), legend.position = c(0.94, 0.80) )+ labs(y = "Value", x= "")+ scale_y_continuous(expand = c(0, 0),limits=c(0, 25))+ facet_grid(.~response,labeller = label_both) # 添加分图标签 tag_facet(plot, fontface = 'bold', tag_pool = c("(a)","(b)"), open = NULL, close = NULL, hjust = -0.05, size = 6)
关键改动说明
- 替换几何对象:将
geom_bar(stat="identity")替换为geom_col_pattern(geom_col是geom_bar(stat="identity")的简写,geom_col_pattern是ggpattern包中支持图案填充的版本)。 - 映射图案变量:在
aes中添加pattern=Time,让Time因子的不同水平对应不同图案。 - 自定义图案样式:通过
scale_pattern_manual为每个Time水平指定图案类型(可选类型包括stripe、circle、crosshatch、grid等),同时调整pattern_fill、pattern_density等参数优化视觉效果。 - 统一图例:由于
fill和pattern都映射到Time因子,图例会自动合并为一个,无需额外设置。
内容的提问来源于stack exchange,提问作者David Moldes
相关产品推荐
相关产品推荐

