使用R的plotly::ggplotly后geom_point图例消失的问题求助
ggplot2转plotly后图例消失的解决思路
问题描述
使用ggplot2生成的散点图图例正常显示,但用plotly::ggplotly()包裹后图例消失,尝试添加plotly::layout(showlegend = T)、移除主题设置均无效。
原始ggplot2代码
library(dplyr) library(ggplot2) set.seed(1) df <- matrix(runif(16, 0, 10), nrow = 4, ncol = 4, dimnames = list(paste0("R", 1:4), paste0("C",1:4))) %>% reshape2::melt() ggplot(df, aes(x = Var2, y = Var1)) + geom_point(aes(size = value), shape = 21, fill = "black", color = "black") + theme_minimal() + theme(axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(angle = 45,vjust = 1, hjust = 1))
环境信息
> sessionInfo() R version 4.2.3 (2023-03-15) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS 14.3 Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] plotly_4.10.4 ggplot2_3.4.4 dplyr_1.1.2 Matrix_1.6-5 loaded via a namespace (and not attached): [1] Rcpp_1.0.10 plyr_1.8.8 pillar_1.9.0 compiler_4.2.3 BiocManager_1.30.20 tools_4.2.3 digest_0.6.31 [8] jsonlite_1.8.4 lifecycle_1.0.3 tibble_3.2.1 gtable_0.3.3 viridisLite_0.4.2 lattice_0.21-8 pkgconfig_2.0.3 [15] rlang_1.1.1 cli_3.6.1 rstudioapi_0.14 crosstalk_1.2.0 yaml_2.3.7 fastmap_1.1.1 stringr_1.5.0 [22] withr_2.5.0 httr_1.4.6 generics_0.1.3 vctrs_0.6.5 htmlwidgets_1.6.2 grid_4.2.3 tidyselect_1.2.0 [29] glue_1.6.2 data.table_1.14.8 R6_2.5.1 fansi_1.0.4 sessioninfo_1.2.2 farver_2.1.1 purrr_1.0.1 [36] tidyr_1.3.0 reshape2_1.4.4 magrittr_2.0.3 ellipsis_0.3.2 scales_1.2.1 htmltools_0.5.7 colorspace_2.1-0 [43] labeling_0.4.2 utf8_1.2.3 stringi_1.7.12 lazyeval_0.2.2 munsell_0.5.0
解决思路
以下是几种可行的解决方法:
方法1:添加绑定size的color映射(强制plotly识别图例)
plotly对仅通过size映射生成的图例支持存在兼容性问题,可临时添加与size绑定的color映射,再用scale_color_identity()保持颜色不变:
library(plotly) p <- ggplot(df, aes(x = Var2, y = Var1)) + geom_point(aes(size = value, color = value), shape = 21, fill = "black") + scale_color_identity() + # 确保实际颜色不受color映射影响 theme_minimal() + theme( axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1) ) ggplotly(p)
方法2:手动构建plotly图例
提取ggplot中的size刻度参数,手动向plotly对象添加图例标记:
library(plotly) p <- ggplot(df, aes(x = Var2, y = Var1)) + geom_point(aes(size = value), shape = 21, fill = "black", color = "black") + theme_minimal() + theme( axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1) ) # 提取ggplot中的size刻度值 size_breaks <- ggplot_build(p)$layout$panel_params[[1]]$size$breaks # 转换为plotly并手动添加图例 ggplotly(p) %>% add_markers( x = NA, y = NA, # 不在图上显示额外点 size = size_breaks, showlegend = TRUE, name = "value", marker = list(color = "black", fill = "black", symbol = 21) ) %>% layout(showlegend = TRUE)
方法3:明确设置size的范围
有时plotly会因size值范围过窄或过宽隐藏图例,手动指定size的显示范围可触发图例显示:
library(plotly) p <- ggplot(df, aes(x = Var2, y = Var1)) + geom_point(aes(size = value), shape = 21, fill = "black", color = "black") + scale_size(range = c(3, 10)) + # 自定义size的最小/最大值 theme_minimal() + theme( axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1) ) ggplotly(p)
内容的提问来源于stack exchange,提问作者dan
相关产品推荐
相关产品推荐

