Reputation: 542
I am trying to create a dynamic map from ggplot2 to plotly.
I tried the following:
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"))
How can I transform this map into a interactive map ? I am trying to following this example (Link)
Using plotly, I couldn`t get the that:
library(plotly)
ggplotly(map)
Error in st_coordinates.sfc(sf::st_geometry(model)) :
not implemented for objects of class sfc_GEOMETRY
Upvotes: 2
Views: 1129
Reputation: 584
The solution is to use the function 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)
See the discussion here: https://github.com/ipeaGIT/geobr/issues/172
Upvotes: 4