r - 使用 plotly 在 R 上创建交互式地图
问题描述
我正在尝试创建从 ggplot2 到 plotly 的动态地图。
我尝试了以下方法:
library(geobr)
library(dplyr)
library(ggplot2)
library(sf)
library(ggthemes)
# download sf of Brazilian states
states <- read_state(code_state = 'all')
map <- ggplot() +
geom_sf(data=states, color="gray90", fill="gray80", size=.4) +
theme_map() +
theme( strip.background = element_rect(colour = "white", fill = "white"),
strip.text.x = element_text(size = 8, face ="bold"))
如何将此地图转换为交互式地图?我正在尝试遵循此示例(链接)
使用情节,我无法得到:
library(plotly)
ggplotly(map)
Error in st_coordinates.sfc(sf::st_geometry(model)) :
not implemented for objects of class sfc_GEOMETRY
解决方案
解决方案是使用函数 sf::st_cast('your geobr map dataset', "MULTIPOLYGON")。
library(plotly)
library(ggplot2)
library(sf)
library(geobr)
library(ggthemes)
states <- read_state(code_state = 'all')
states = sf::st_cast(states, "MULTIPOLYGON")
map <- ggplot() +
geom_sf(data=states, color="gray90", fill="gray80", size=.4) +
theme_map() +
theme( strip.background = element_rect(colour = "white", fill = "white"),
strip.text.x = element_text(size = 8, face ="bold"))
ggplotly(map)
推荐阅读
- django - 在 django 中使用 Tweepy 进行身份验证并保存用户的访问令牌和访问令牌秘密
- html - 修复了溢出视口时模态不会滚动的问题
- dart - 导入“包:using_tabs/tabs/first.dart”;>> URI 的目标不存在
- r - R - Boxplot x 轴不带刻度且完整
- javascript - iframe src 属性不起作用
- css - 使用 flex 垂直对齐项目
- java - 如何从 void 方法传输变量?
- symfony - nelmioApiDocBundle 是否支持 symfony 序列化规范化器和名称转换器?
- angular - Angular 应用程序部署到 firebase:404 错误
- python - Atom Hydrogen - 安装干净的 python 后的多个内核