使用ggpubr::ggarrange组合含ggpattern的3幅图时出现seq.default错误
问题:ggarrange组合含ggpattern的多图时抛出序列错误
使用ggarrange组合3幅图(其中1幅是带ggpattern图案的ggplot图)时,出现错误:Error in seq.default(from, to, by) : invalid '(to - from)/by'。但仅组合带图案的图和其中1幅无图案图时可正常运行。
简化示例代码如下:
# 模拟数据 data <- structure(list(Level= c(0.2, 0.3, 0.25, 0.35, 0.4, 0.5, 0.5, 0.6, 0.15, 0.35), Group= c("A", "A", "B", "B", "C", "C", "D", "D", "E", "E"), Condition = c("no", "yes", "no", "yes", "no", "yes", "no", "yes", "no", "yes"), Hx = c(0,1,1,1,0,1,0,0,0,0), Type = c("T", "T", "T", "T", "T", "F", "F", "F", "F", "F")), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")) # 创建带图案的图 pattern_plot <- ggplot(data = data, aes(x = Group, y = Level)) + facet_grid(cols=vars(Type)) + geom_bar_pattern(aes(pattern = Condition, fill = as.factor(Hx)), stat = "identity", position = "dodge", color = "black", pattern_fill = "black", pattern_angle = 45, pattern_density = 0.1, pattern_spacing = 0.025, pattern_key_scale_factor = 0.6) + scale_pattern_manual(values = c("none", "stripe")) + labs(x = "", y = "", pattern = "Condition", fill = "History", subtitle = "Percentage of people with condition") + guides(pattern = guide_legend(override.aes = list(fill = "white")), fill = guide_legend(override.aes = list(pattern = "none"))) + theme_bw() + theme(legend.position="left") + scale_y_continuous(labels = scales::percent_format(accuracy = 1)) # 创建无图案图1 no_pattern_plot <- ggplot(data = data, aes(x = Group, y = Level, fill = Condition)) + geom_bar(position = "dodge", stat = "identity") + theme_bw() + scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + theme(legend.position="left") # 创建无图案图2 no_pattern_plot2 <- ggplot(data = data, aes(x = Group, y = Level, fill = as.factor(Hx))) + geom_bar(position = "dodge", stat = "identity") + theme_bw() + scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + theme(legend.position="left")
可正常运行的代码:
ggarrange(pattern_plot, no_pattern_plot2, nrow = 2)
报错的代码:
ggarrange(pattern_plot, no_pattern_plot, no_pattern_plot2, nrow = 3)
解决思路
这个错误是ggarrange在协调不同图例(尤其是ggpattern新增的pattern图例)的布局时出现了计算问题,以下是几种可行的解决方法:
方法1:统一所有图的图例类型
给仅含fill图例的无图案图添加空的pattern图例,让ggarrange能统一处理图例布局:
# 给no_pattern_plot添加空的pattern图例 no_pattern_plot <- no_pattern_plot + scale_pattern_manual(values = c("none")) + labs(pattern = "Condition") + guides(pattern = guide_legend(override.aes = list(fill = "white"))) # 给no_pattern_plot2添加空的pattern图例 no_pattern_plot2 <- no_pattern_plot2 + scale_pattern_manual(values = c("none")) + labs(pattern = "Condition") + guides(pattern = guide_legend(override.aes = list(fill = "white"))) # 执行组合 ggarrange(pattern_plot, no_pattern_plot, no_pattern_plot2, nrow = 3)
方法2:单独提取图例,统一布局
将所有图的图例合并后单独放置,避免ggarrange在协调图例时出错:
# 提取pattern_plot的完整图例 combined_legend <- get_legend(pattern_plot) # 移除所有图的图例 pattern_plot_noleg <- pattern_plot + theme(legend.position = "none") no_pattern_plot_noleg <- no_pattern_plot + theme(legend.position = "none") no_pattern_plot2_noleg <- no_pattern_plot2 + theme(legend.position = "none") # 先组合无图例的图,再添加统一图例 ggarrange( ggarrange(pattern_plot_noleg, no_pattern_plot_noleg, no_pattern_plot2_noleg, nrow = 3), combined_legend, widths = c(3, 1) )
方法3:改用patchwork包组合图
patchwork对含ggpattern的ggplot图兼容性更好,能避免这类布局计算错误:
library(patchwork) # 直接按行组合 pattern_plot / no_pattern_plot / no_pattern_plot2
内容的提问来源于stack exchange,提问作者ekazura
相关产品推荐
相关产品推荐

