Hi, im trying to make a custom choropleth map of Brazil with hoverinfo. To do so, im using a shapefile as mentioned on another topic on this forum. The custom choropleth part of my goal is already reached but i can’t figure out how to make a hoverinfo other than one showing the number used to color the map.
I tryed following the example on this link:
And so this is my resulting code:
library(plotly)
library(sp)
library(rgdal)
library(tidyverse)
#shapefile available at https://www.arcgis.com/home/item.html?id=1e45bf47c2f542cda9d507ce38ba354a
shape <- readOGR(dsn = “~/new/Brazil_Admin_1.shp”,layer = “Brazil_Admin_1”)
a <- shape@polygons
b <- data.frame()
for (i in c(1:20,22:length(a))){
b <- rbind(b,cbind(a[[i]]@Polygons[[length(a[[i]]@Polygons)]]@coords,rep(i,nrow(a[[i]]@Polygons[[length(a[[i]]@Polygons)]]@coords))))
}
b <- rbind(b,cbind(a[[21]]@Polygons[[1]]@coords,21))
name <- rep(NA,nrow(b))
name[b$V3 == 1] <- "Acre"
name[b$V3 == 2] <- "Amazonas"
name[b$V3 == 23] <- "Roraima"
name[b$V3 == 5] <- "Rondônia"
name[b$V3 == 4] <- "Pará"
name[b$V3 == 24] <- "Amapá"
name[b$V3 == 6] <- "Tocantins"
name[b$V3 == 3] <- "Maranhão"
name[b$V3 == 15] <- "Piauí"
name[b$V3 == 14] <- "Ceará"
name[b$V3 == 21] <- "Rio Grande do Norte"
name[b$V3 == 19] <- "Paraíba"
name[b$V3 == 27] <- "Pernambuco"
name[b$V3 == 16] <- "Alagoas"
name[b$V3 == 22] <- "Sergipe"
name[b$V3 == 17] <- "Bahia"
name[b$V3 == 10] <- "Mato Grosso"
name[b$V3 == 8] <- "Mato Grosso do Sul"
name[b$V3 == 25] <- "Goiás"
name[b$V3 == 7] <- "Distrito Federal"
name[b$V3 == 9] <- "Minas Gerais"
name[b$V3 == 18] <- "Espirito Santo"
name[b$V3 == 20] <- "Rio de Janeiro"
name[b$V3 == 26] <- "São Paulo"
name[b$V3 == 12] <- "Paraná"
name[b$V3 == 13] <- "Santa Catarina"
name[b$V3 == 11] <- “Rio Grande do Sul”
b <- cbind(b,name)
names(b) <- c(“long”,“lat”, “state”, “description”)
b$state <- as.factor(b$state)
p <- b %>%
group_by(state) %>%
plot_ly(
width = 600, height = 600
, x = ~long, y = ~lat
, color = ~state, text = ~description , hoverinfo = ‘text’
, showlegend = F
) %>%
add_polygons(line = list(width = 0.4)) %>%
add_polygons(
fillcolor = ‘transparent’,
line = list(color = ‘black’, width = 0.5),
showlegend = FALSE
, text = ~description, hoverinfo = ‘text’
) %>%
layout(title = ‘Brazilian States’,# geo = g,
titlefont = list(size = 10),
xaxis = list(title = “”, showgrid = FALSE,
zeroline = FALSE, showticklabels = FALSE),
yaxis = list(title = “”, showgrid = FALSE,
zeroline = FALSE, showticklabels = FALSE)
)
p
Does anyone know how to do this?