我正在尝试创建从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"))
我如何将该地图转换为交互式地图?我正在尝试遵循此示例(Link)
使用密谋,我无法得到那个:
library(plotly)
ggplotly(map)
Error in st_coordinates.sfc(sf::st_geometry(model)) :
not implemented for objects of class sfc_GEOMETRY
解决方案是使用函数sf :: st_cast(“您的geobr地图数据集”,“ 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)