r - 将标签添加到使用 plot_geo 创建的绘图地图
问题描述
我有一个使用plotly::plot_geo
. 我想在地图顶部添加标签,例如,在地图上阿拉巴马州的位置上,它会显示“AL (68)”,但对于所有州,如下例所示:
谁能告诉我是否有办法做到这一点?
library(tidyverse)
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_geo() %>%
add_trace(
z = ~density, text = state.name, span = I(0),
locations = state.abb, locationmode = 'USA-states'
) %>%
layout(geo = g)
解决方案
使用您的示例,这可以通过plotly::plot_ly()
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, text = state.name,
color = ~density, autocolorscale = TRUE) %>%
add_trace(type = "scattergeo", locationmode = 'USA-states',
locations = state.abb, text = paste0(state.abb, "\n", density),
mode = "text",
textfont = list(color = rgb(0,0,0), size = 12))
输出是:
仍然不确定如何使用 来执行此操作plotly::plot_geo()
,但此解决方案确实允许您留在情节家庭中。
推荐阅读
- javascript - 回调的返回值为空。如何构建我的代码以正确获取返回值?
- node.js - 如何在响应 AJAX 请求之前等待 Express 中的异步响应?
- javascript - 如何将一串pdf代码转换为blob?
- c - 如何将文件指针从 c 传递给 asm 中的调用
- spring-boot - Spring boot(2.2 MI) Security : 为 HttpBasic 配置自定义 AuthenticationFailureHandler
- rust - 如何找到“派生”中使用的符号的定义位置?
- linux - 如何使用 Perl 脚本列出文件的行号?
- vhdl - 循环线和线的位置
- python - Tkinter .after 模块只是延迟了 GUI 的打开
- reactjs - Bootstrap carousal 在生产应用程序中不起作用,但在本地工作