R Plotly choropleth地图如何将密度图例移至底部水平显示或隐藏色条
解决方案
问题根因
你之前调整的layout.legend参数仅对离散分类的图例生效,等值线图的连续密度色条属于独立的colorbar控件,需要在trace层级单独配置。
水平显示色条到底部的实现方案
直接在add_trace中新增colorbar参数配置朝向和位置即可,完整可运行代码如下:
library(dplyr) library(plotly) set.seed(1) density <- sample(1:100, 50, replace = T) g <- list( scope = 'usa', projection = list(type = 'albers usa'), lakecolor = toRGB('white') ) plot_ly() %>% layout(geo = g) %>% add_trace(type = "choropleth", locationmode = 'USA-states', locations = state.abb, z = ~density, color = ~density, # 以下为色条配置 colorbar = list( orientation = "h", x = 0.5, xanchor = "center", y = -0.1, yanchor = "bottom", title = list(text = "密度", side = "bottom") ))
参数调整说明
- 如需调整色条和地图的间距,修改
y参数即可,数值越小位置越靠下 - 如果你仅需要隐藏色条,直接在trace中添加
showscale = F即可
内容的提问来源于stack exchange,提问作者DJC
相关产品推荐
相关产品推荐

