How to make a map of Brazil

Asked

Viewed 612 times

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)
  • 3

    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.

1 answer

0

If the idea is merely to create a map of Brazil, the following code produces it in a very simple way:

library(geobr)
library(ggplot2)
library(cowplot)

uf <- read_state(code_state = "all")

no_axis <- theme(axis.title=element_blank(),
                 axis.text=element_blank(),
                 axis.ticks=element_blank())

plot_grafico <- ggplot() + 
  geom_sf(data=uf, fill="white", color="black", size=.15, show.legend = FALSE) +
  theme_nothing() +
  no_axis +
  labs(subtitle="Terras indígenas", size=8)

plot_grafico

The function no_axis is merely to remove the axes and make the image simple. geo_br package allows you to produce several maps. I recommend exploring the features.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.