O daltonismo, pelo menos pra mim, torna a busca por paletas de cores
uma tarefa interessante. Esse post é apenas um lembrete de acesso rápido
sobre as paletas disponíveis no R por padrão. Eu mostro como utilizar em
gráficos (por exemplo, no {ggplot2}
) e como extrair os
códigos hexadecimais das cores das paletas. Vale a pena dar uma olhada
aqui também.
Pacotes necessários:
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("grDevices")) install.packages("grDevices")
if (!require("dplyr")) install.packages("dplyr")
if (!require("scales")) install.packages("scales")
if (!require("glue")) install.packages("glue")
if (!require("sf")) install.packages("sf")
if (!require("rnaturalearth")) install.packages("rnaturalearth")
if (!require("rnaturalearthdata")) install.packages("rnaturalearthdata")
# tema dos plots
theme_set(theme_void())
# dados utilizados
world <- ne_countries(scale = "medium", returnclass = "sf")
As categorias de paletas disponíveis são essas:
pals <- c("divergingx", "qualitative", "diverging", "sequential")
Vetor identificado com os nomes das paletas:
available_palettes <- c()
for (i in seq_along(pals)) {
pals_vec <- hcl.pals(type = pals[i])
pals_names <- rep(pals[i], length(pals_vec))
names(pals_vec) <- pals_names
available_palettes <- c(available_palettes, pals_vec)
}
Um total de 115 paletas disponíveis!
available_palettes
## divergingx divergingx divergingx divergingx divergingx
## "ArmyRose" "Earth" "Fall" "Geyser" "TealRose"
## divergingx divergingx divergingx divergingx divergingx
## "Temps" "PuOr" "RdBu" "RdGy" "PiYG"
## divergingx divergingx divergingx divergingx divergingx
## "PRGn" "BrBG" "RdYlBu" "RdYlGn" "Spectral"
## divergingx divergingx divergingx qualitative qualitative
## "Zissou 1" "Cividis" "Roma" "Pastel 1" "Dark 2"
## qualitative qualitative qualitative qualitative qualitative
## "Dark 3" "Set 2" "Set 3" "Warm" "Cold"
## qualitative qualitative diverging diverging diverging
## "Harmonic" "Dynamic" "Blue-Red" "Blue-Red 2" "Blue-Red 3"
## diverging diverging diverging diverging diverging
## "Red-Green" "Purple-Green" "Purple-Brown" "Green-Brown" "Blue-Yellow 2"
## diverging diverging diverging diverging diverging
## "Blue-Yellow 3" "Green-Orange" "Cyan-Magenta" "Tropic" "Broc"
## diverging diverging diverging diverging diverging
## "Cork" "Vik" "Berlin" "Lisbon" "Tofino"
## sequential sequential sequential sequential sequential
## "Grays" "Light Grays" "Blues 2" "Blues 3" "Purples 2"
## sequential sequential sequential sequential sequential
## "Purples 3" "Reds 2" "Reds 3" "Greens 2" "Greens 3"
## sequential sequential sequential sequential sequential
## "Oslo" "Purple-Blue" "Red-Purple" "Red-Blue" "Purple-Orange"
## sequential sequential sequential sequential sequential
## "Purple-Yellow" "Blue-Yellow" "Green-Yellow" "Red-Yellow" "Heat"
## sequential sequential sequential sequential sequential
## "Heat 2" "Terrain" "Terrain 2" "Viridis" "Plasma"
## sequential sequential sequential sequential sequential
## "Inferno" "Rocket" "Mako" "Dark Mint" "Mint"
## sequential sequential sequential sequential sequential
## "BluGrn" "Teal" "TealGrn" "Emrld" "BluYl"
## sequential sequential sequential sequential sequential
## "ag_GrnYl" "Peach" "PinkYl" "Burg" "BurgYl"
## sequential sequential sequential sequential sequential
## "RedOr" "OrYel" "Purp" "PurpOr" "Sunset"
## sequential sequential sequential sequential sequential
## "Magenta" "SunsetDark" "ag_Sunset" "BrwnYl" "YlOrRd"
## sequential sequential sequential sequential sequential
## "YlOrBr" "OrRd" "Oranges" "YlGn" "YlGnBu"
## sequential sequential sequential sequential sequential
## "Reds" "RdPu" "PuRd" "Purples" "PuBuGn"
## sequential sequential sequential sequential sequential
## "PuBu" "Greens" "BuGn" "GnBu" "BuPu"
## sequential sequential sequential sequential sequential
## "Blues" "Lajolla" "Turku" "Hawaii" "Batlow"
Vou utilizar as estimativas de tamanho populacional por países para exibir as paletas:
for (i in seq_along(available_palettes)) {
plot_pals <-
ggplot() +
geom_sf(data = world, aes(fill = as.numeric(pop_est))) +
scale_fill_gradientn(
colours = hcl.colors(
n = 20,
palette = paste(available_palettes[i])
),
labels = comma_format(),
n.breaks = 10,
breaks = breaks_extended(n = 10)
) +
labs(
title = glue("hcl.pals(type = '{names(available_palettes[i])}')"),
subtitle = glue("código para usar essa paleta: `grDevices::hcl.colors(n = 20, palette = '{available_palettes[i]}')`"),
fill = "Tamanho populacional"
)
print(plot_pals)
}
Escolho a paleta e utilizo no plot, por exemplo:
pop_map <-
ggplot() +
geom_sf(data = world, aes(fill = as.numeric(pop_est))) +
scale_fill_gradientn(
colours = hcl.colors(
n = 20,
palette = "Viridis" # paleta escolhida
),
labels = comma_format(),
n.breaks = 10,
breaks = breaks_extended(n = 10)
)
print(pop_map)
Posso também extrair os códigos hexadecimais das cores da paleta e usar em outro programa:
grDevices::hcl.colors(n = 20, "Viridis")
## [1] "#4B0055" "#491562" "#432A6F" "#383B7A" "#214C85" "#005C8D" "#006B93"
## [8] "#007A96" "#008898" "#009597" "#00A193" "#00AD8D" "#00B884" "#00C279"
## [15] "#46CA6B" "#76D25A" "#9CD948" "#BFDE36" "#DFE22B" "#FDE333"