This is an R Markdown document. The following code shows how to create a map with plotly and using geographical data in geojson format and demographic data in csv format.
library(plotly)
#install.packages("RJSONIO")
library(RJSONIO)
#url = 'https://github.com/juaneladio/peru-geojson/blob/master/peru_departamental_simple.geojson'
departamentos = RJSONIO::fromJSON(content = "peru_departamental_simple.geojson")#
peru_population = read.csv("peru_pop_dep.csv")
fig <- plot_ly() %>% add_trace(
type="choropleth",
colorscale = "Viridis",
featureidkey = "properties.NOMBDEP",
# ---- data ----------------------------
geojson = departamentos,
locations = peru_population$NOMBDEP,
z = peru_population$POBLACION
# --------------------------------------
)
g <- list(fitbounds = "locations",visible = FALSE)
fig <- fig %>% layout(geo = g, title ="2021 Perú's Population" ) %>% colorbar(title = "Population")
fig