2
I’m trying to make a map on the R for the first time. I did all the necessary process with the data and am following the instructions of a script available here but he does not recognize the object "Longest".
You can help?
The script looks like this:
#install.packages("pacman")
rm(list=ls())
pacman::p_load(tidyverse, janitor, haven, forcats,
formattable,
knitr,stringr, hrbrthemes,
rgdal, mapproj, plotly)
CP <- read.table("/Users/marciarangelcandido/CP_UF.csv", sep = ";", header = TRUE)
CP <- CP %>%
mutate(Estado = case_when(Estado == "A3re" ~ "AC",
TRUE ~ as.character(Estado))) %>%
rename("sigla" = "Estado")
mapa <- read.csv("https://raw.githubusercontent.com/thiago-ms-cp/programacao_iesp/master/mapa_est_geral.csv")
library(descr)
mapa$sigla <- toUTF8(mapa$sigla, "IBM850")
#palette(c("#779999", "#99bbbb", "#bbdddd", "#ddffff"))
#plot(mapa, col = mapa$sigla)
#title("Mapa de Publicações da Ciência Política")
CP <- CP %>%
select(sigla, Estrato)
mapa <- mapa %>%
left_join(CP, by = "sigla")
mapa<-mapa %>%
group_by(sigla) %>%
mutate(a1= sum(Estrato==10)) %>%
mutate(a2=sum(Estrato==9)) %>%
mutate(b1=sum(Estrato==8)) %>%
mutate(b2=sum(Estrato==7)) %>%
mutate(b3=sum(Estrato==6)) %>%
mutate(b4=sum(Estrato==5)) %>%
mutate(b5=sum(Estrato==4)) %>%
mutate(c=sum(Estrato==3))
mapa$texto <- paste("A1 =", CP$a1, "revistas",
"; A2 =", CP$a2, "revistas",
"; B1 =", CP$b1, "revistas",
"; B2 =", CP$b2, "revistas",
"; B3 =", CP$b3, "revistas",
"; B4 =", CP$b4, "revistas",
"; B5 =", CP$b5, "revistas",
"; C =", CP$c, "revistas")
mapa %>%
plot_ly(~longest, ~latest, group = ~group)
plot_ly(mapa, lon = **longest**, lat = latest, text = texto,
color = Estrato, type = 'scattergeo', locationmode = 'country names') %>%
layout(title = 'Populations<br>(Click legend to toggle)')
mapa$Estado <- cut(mapa$Estado, 4)
levels(mapa$Estado)
The difficulty is that your example is not reproducible. The base CP_UF has not been made available. Also, I suggest you take a look at the documentation of the sf package. It will make your life much easier to handle maps. plotly accepts simple Features (sf) and most htmlwidgets as well.
– José