Coloring map with ggplot_2

Asked

Viewed 55 times

1

Vi that question here in the so and tried to reproduce to my situation, however, still I did not get the desired result.

library(tidyverse)
library(geobr)
library(RColorBrewer)

Below the map of Brazil

br<-read_state(code_state = "all",
               year = 2010)

I import the data

The data I use is at that link. It is of the site of open data of the Ministry of Tourism

As the purpose is to make a data.frames Join, I take the data import process to apply the function rename().

emendas<-rio::import('ementas_parlamentares_dados2017_2018.xlsx') %>% 
  janitor::clean_names() %>% 
  rename(abbrev_state = uf)

I do the Join

emendas_br<- inner_join(br, emendas, by ="abbrev_state")

I plot the map trying to replicate the method used in that answer I mentioned:

Here trying to apply 2 colors with scale_fill_gradient():

ggplot()+
  geom_sf(data = emendas_br, aes(fill = valor_global), color = NA)+
  scale_fill_gradient(low = "white", high = "#2D3E50", name = "V.Global", limits = c(0, 14500000))

Usando 2 cores com scale_fill_gradient()

I also tried using a color palette:

Here applying the "Spectral" palette of RColorBrewer() with scale_fill_gradientn()

ggplot()+
  geom_sf(data = emendas_br, aes(fill = valor_global), color = NA)+
  scale_fill_gradientn(colors = brewer.pal(9,"Spectral"))

Usando a paleta "Spectral" do RColorBrewer() com scale_fill_gradientn()

My doubt:

Why the colors of the higher levels of the scale are not represented on the map?

For example, Rondônia is the only state of the dataset that has the maximum "global value" ("14500000").

I expected him to appear in the color equivalent to the full scale

1 answer

2


The scale is correct, it’s just that a lot of the data isn’t showing up on the map. The data are by municipality; when making the union each state is with several lines, each corresponding to a polygon. What appears in the final map is the last of them of each UF, superimposed on the others.

To correct, summarize the data by UF before joining:

emendas_br <- emendas %>%
  group_by(abbrev_state) %>%
  summarize(total = sum(valor_global)) %>%
  left_join(br, .)

ggplot(emendas_br) +
  geom_sf(aes(fill = total/1e6)) +  # dividi por 1e6 para facilitar a leitura da legenda
  scale_fill_gradient("V. Global total\n(em milhões)", low = "white", high = "#2D3E50") +
  theme_void()

inserir a descrição da imagem aqui

  • Thank you very much for the excellent explanations Carlos Eduardo Lagosta

Browser other questions tagged

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