使用Rally在R上创建交互式地图

问题描述 投票:0回答:1

我正在尝试创建从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"))

Map

我如何将该地图转换为交互式地图?我正在尝试遵循此示例(Link

使用密谋,我无法得到那个:

library(plotly)
ggplotly(map)

Error in st_coordinates.sfc(sf::st_geometry(model)) : 
  not implemented for objects of class sfc_GEOMETRY
r dictionary ggplot2 plotly
1个回答
0
投票

解决方案是使用函数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)

请参见此处的讨论:https://github.com/ipeaGIT/geobr/issues/172

© www.soinside.com 2019 - 2024. All rights reserved.