使用tidybayes包stat_pointinterval()时出现图层美学设置错误
问题
之前使用tidybayes包的stat_pointinterval()绘制模型参数的中位数及可信区间图,现在所有绘图均失败,报错如下:
Error in `layer_slabinterval()`: ! Problem while setting up geom aesthetics. ℹ Error occurred in the 1st layer.
即使是去除所有参数的最简示例也无法运行:
df <- data.frame(x = rep(LETTERS[1:2]), y = c(2:11)) ggplot(data = df, aes(x = x, y = y)) + stat_pointinterval()
附sessionInfo()信息:
R version 4.4.2 (2024-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows 11 x64 (build 22631) Matrix products: default locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 [4] LC_NUMERIC=C LC_TIME=English_United States.utf8 time zone: America/Halifax tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] ggdist_3.3.2 ggpubr_0.6.0 ggh4x_0.3.0 ggrepel_0.9.5 [5] rnaturalearthhires_1.0.0.9000 rnaturalearth_1.0.1 bayesplot_1.11.1 patchwork_1.3.2 [9] ggnewscale_0.4.10 rstantools_2.4.0 tidybayes_3.0.6 brms_2.21.0 [13] Rcpp_1.0.12 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 [17] dplyr_1.1.4 purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 [21] tibble_3.2.1 ggplot2_4.0.0 tidyverse_2.0.0 plyr_1.8.9 loaded via a namespace (and not attached): [1] DBI_1.2.3 gridExtra_2.3 inline_0.3.19 sandwich_3.1-0 rlang_1.1.4 magrittr_2.0.3 [7] multcomp_1.4-25 matrixStats_1.3.0 e1071_1.7-16 compiler_4.4.2 loo_2.7.0 reshape2_1.4.4 [13] callr_3.7.6 vctrs_0.6.5 crayon_1.5.3 pkgconfig_2.0.3 arrayhelpers_1.1-0 backports_1.4.1 [19] labeling_0.4.3 utf8_1.2.4 tzdb_0.4.0 ps_1.9.1 jsonlite_1.8.8 terra_1.7-78 [25] broom_1.0.6 parallel_4.4.2 R6_2.6.1 stringi_1.8.4 RColorBrewer_1.1-3 StanHeaders_2.32.7 [31] pkgload_1.4.0 parallelly_1.37.1 car_3.1-2 estimability_1.5.1 rstan_2.32.6 future.apply_1.11.2 [37] zoo_1.8-12 Matrix_1.7-1 splines_4.4.2 timechange_0.3.0 tidyselect_1.2.1 rstudioapi_0.16.0 [43] abind_1.4-5 codetools_0.2-20 processx_3.8.6 listenv_0.9.1 pkgbuild_1.4.7 lattice_0.22-6 [49] withr_3.0.2 bridgesampling_1.1-2 S7_0.2.0 posterior_1.5.0 coda_0.19-4.1 future_1.33.2 [55] survival_3.7-0 sf_1.0-20 units_0.8-7 proxy_0.4-27 RcppParallel_5.1.7 pillar_1.10.2 [61] carData_3.0-5 tensorA_0.36.2.1 KernSmooth_2.23-24 checkmate_2.3.1 stats4_4.4.2 distributional_0.4.0 [67] generics_0.1.3 sp_2.2-0 hms_1.1.3 scales_1.4.0 globals_0.16.3 xtable_1.8-4 [73] class_7.3-22 glue_1.8.0 emmeans_1.10.2 tools_4.4.2 data.table_1.15.4 ggsignif_0.6.4 [79] mvtnorm_1.2-4 cowplot_1.1.3 grid_4.4.2 QuickJSR_1.1.3 colorspace_2.1-1 nlme_3.1-166 [85] cli_3.6.3 svUnit_1.0.6 viridisLite_0.4.2 Brobdingnag_1.2-9 gtable_0.3.6 rstatix_0.7.2 [91] digest_0.6.35 classInt_0.4-11 TH.data_1.1-2 farver_2.1.2 lifecycle_1.0.4 httr_1.4.7 [97] MASS_7.3-61
解决方案
1. 解决plyr与dplyr的冲突
你同时加载了plyr_1.8.9和dplyr_1.1.4,这两个包的核心函数(如summarise)存在命名冲突。stat_pointinterval()依赖ggdist包,而ggdist内部使用dplyr的函数进行数据汇总,plyr的同名函数会覆盖dplyr的实现,导致绘图时出现美学映射错误。
解决方法:
- 在运行绘图代码前,先卸载plyr:
detach(package:plyr, unload = TRUE) - 或者调整包的加载顺序,确保dplyr在plyr之后加载,让dplyr的函数覆盖plyr的;
- 长期方案:避免同时加载这两个包,改用dplyr的函数完成所有数据处理工作。
2. 确认数据结构与映射正确
stat_pointinterval()默认会对每个x分组计算y的中位数及可信区间,你的最简示例数据结构是正确的(每个x对应多个y值),但需确保:
- 数据中每个分组(
x的每个类别)有足够多的观测值用于计算区间; - 若使用模型输出的 posterior 样本,需确保数据是长格式(每个参数样本占一行),并正确映射分组变量。
3. 测试修正后的最简示例
在解决包冲突后,运行以下代码(确保x的每个类别对应多个y值):
df <- data.frame(x = rep(LETTERS[1:2], each = 5), y = 2:11) ggplot(data = df, aes(x = x, y = y)) + stat_pointinterval()
应该能正常生成包含中位数点和可信区间的图形。
内容的提问来源于stack exchange,提问作者tnt
相关产品推荐
相关产品推荐

