Coloring a map with different color depths in ggplot2

247 Views Asked by At

I'm trying to plot a map of brazil colored with a specific color, "#2D3E50". However, using this color on the map to color from the smallest area (light shade) to the largest area (dark shade) is not working. See below the chosen color that I am trying to insert for the different proportions.

library(geobr)
library(ggplot2)
library(cowplot)
library(RColorBrewer)
library(dplyr)

dados <- structure(
  list(X = 1:27, 
      uf = c("Acre", "Alagoas", "Amapá", 
      "Amazônas", "Bahia", "Ceará", "Distrito Federal", "Espírito Santo", 
      "Goiás", "Maranhão", "Mato Grosso do Sul", "Mato Grosso", "Minas Gerais", 
      "Paraíba", "Paraná", "Pará", "Pernambuco", "Piauí", "Rio de Janeiro", 
      "Rio Grande do Norte", "Rio Grande do Sul", "Rondônia", "Roraima", 
      "Santa Catarina", "São Paulo", "Sergipe", "Tocantins"), 
      AreaTotal = c(0, 0.01, 0.07, 0, 0.6, 0, 0, 0.23, 0.14, 0.24, 1.14, 0.6, 1.96, 
                    0, 1.01, 0.21, 0, 0.03, 0.03, 0, 0.83, 0.03, 0.03, 0.64, 1.4, 
                    0, 0.15)), class = "data.frame", row.names = c(NA, -27L))
states <- read_state(code_state = "all",year = 2019)
states$name_state <- tolower(states$name_state)
dados$uf <- tolower(dados$uf)

states <- dplyr::left_join(states, dados, by = c("name_state" = "uf")); states

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

ggplot() + 
  geom_sf(data=states, aes(fill = AreaTotal), color=NA, size=.15) +
  no_axis + labs(size=8) + scale_fill_distiller(palette = "2D3E50", name="Áreas", limits = c(0,2))

Warning message:
In pal_name(palette, type) : Unknown palette 2D3E50

See that it is automatically filling with green colors, and more, the colors are reversed since the darker ones are showing the lower areas.

1

There are 1 best solutions below

0
On BEST ANSWER

The RColorBrewer package does not contain a color palette called # 2D3E50. Therefore, it is enough to elaborate a sequence of colors with their respective shades, that is, a color for low values ​​and high values, being these: low = "white", high = "#2D3E50".

ggplot() + 
  geom_sf(data = states, aes(fill = AreaTotal)) +
  no_axis + 
  labs(size = 8) + 
  scale_fill_gradient(low = "white", high = "#2D3E50", name = "Áreas", limits = c(0, 2))