Coloring a map with differing color intensity in ggplot2

Asked

Viewed 85 times

3

I’m trying to create the Plot of a color map of Brazil with a specific color, the "#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 I’m trying to insert for the different proportions.

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

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 automatically it is filling with green colors, and more, the colors are inverted since the darker ones are presenting the lower areas.

1 answer

2


The function scale_fill_distiller does not have a color palette called #2D3E50, for the package RColorBrewer does not define that palette. To check the available palettes, turn the command ?RColorBrewer.

If the goal is to leave a light color (as white) and reach the color #2D3E50, recommend to use scale_fill_gradient. To create a sequence of colors for this function, just define what is the color for low values (low) and high values (high), that she herself is responsible for creating the intermediate colours.

library(geobr)
#> Loading required namespace: sf
library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

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)
#> Using year 2019
#> Loading data for the whole country
#>   |                                                                              |                                                                      |   0%  |                                                                              |===                                                                   |   4%  |                                                                              |=====                                                                 |   7%  |                                                                              |========                                                              |  11%  |                                                                              |==========                                                            |  15%  |                                                                              |=============                                                         |  19%  |                                                                              |================                                                      |  22%  |                                                                              |==================                                                    |  26%  |                                                                              |=====================                                                 |  30%  |                                                                              |=======================                                               |  33%  |                                                                              |==========================                                            |  37%  |                                                                              |=============================                                         |  41%  |                                                                              |===============================                                       |  44%  |                                                                              |==================================                                    |  48%  |                                                                              |====================================                                  |  52%  |                                                                              |=======================================                               |  56%  |                                                                              |=========================================                             |  59%  |                                                                              |============================================                          |  63%  |                                                                              |===============================================                       |  67%  |                                                                              |=================================================                     |  70%  |                                                                              |====================================================                  |  74%  |                                                                              |======================================================                |  78%  |                                                                              |=========================================================             |  81%  |                                                                              |============================================================          |  85%  |                                                                              |==============================================================        |  89%  |                                                                              |=================================================================     |  93%  |                                                                              |===================================================================   |  96%  |                                                                              |======================================================================| 100%
states$name_state <- tolower(states$name_state)
dados$uf <- tolower(dados$uf)

states <- left_join(states, dados, by = c("name_state" = "uf")); states
#> Simple feature collection with 27 features and 7 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -73.99045 ymin: -33.75118 xmax: -28.84784 ymax: 5.271841
#> geographic CRS: SIRGAS 2000
#> First 10 features:
#>    code_state abbrev_state name_state code_region name_region  X AreaTotal
#> 1          11           RO   rondônia           1       Norte 22      0.03
#> 2          12           AC       acre           1       Norte  1      0.00
#> 3          13           AM   amazônas           1       Norte  4      0.00
#> 4          14           RR    roraima           1       Norte 23      0.03
#> 5          15           PA       pará           1       Norte 16      0.21
#> 6          16           AP      amapá           1       Norte  3      0.07
#> 7          17           TO  tocantins           1       Norte 27      0.15
#> 8          21           MA   maranhão           2    Nordeste 10      0.24
#> 9          22           PI      piauí           2    Nordeste 18      0.03
#> 10         23           CE      ceará           2    Nordeste  6      0.00
#>                              geom
#> 1  MULTIPOLYGON (((-65.3815 -1...
#> 2  MULTIPOLYGON (((-71.07772 -...
#> 3  MULTIPOLYGON (((-69.83766 -...
#> 4  MULTIPOLYGON (((-63.96008 2...
#> 5  MULTIPOLYGON (((-51.43248 -...
#> 6  MULTIPOLYGON (((-50.45011 2...
#> 7  MULTIPOLYGON (((-48.23163 -...
#> 8  MULTIPOLYGON (((-44.5383 -2...
#> 9  MULTIPOLYGON (((-42.91539 -...
#> 10 MULTIPOLYGON (((-41.18292 -...

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_gradient(low = "white", high = "#2D3E50", name = "Áreas", limits = c(0, 2))

Knowing a bit of hexadecimal colors, you can change the initial color, so it starts in a lighter shade of blue and not exactly white.

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

Personally, I didn’t like the use of color = NA inside geom_sf. States that have very similar levels for AreaTotal end up with borders almost indistinguishable. If this is the goal, I see no problem in leaving it so. On the other hand, it might be more interesting to color the border lines so that states are more detached from each other.

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

Created on 2021-02-25 by the reprex package (v1.0.0)

  • 1

    Thank you very much Marcus!!

Browser other questions tagged

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