# install.packages(c("viridis", "RColorBrewer", "wesanderson", "ggsci", "ggplot2", "patchwork"))
library(ggplot2)
library(viridis)
library(RColorBrewer)
library(wesanderson)
library(ggsci)
# Przykładowe dane
set.seed(123)
df <- data.frame(
kategoria = rep(LETTERS[1:5], each = 20),
x = rnorm(100),
y = rnorm(100)
)
head(df)
## kategoria x y
## 1 A -0.56047565 -0.71040656
## 2 A -0.23017749 0.25688371
## 3 A 1.55870831 -0.24669188
## 4 A 0.07050839 -0.34754260
## 5 A 0.12928774 -0.95161857
## 6 A 1.71506499 -0.04502772
{VIRIDIS} - palety przyjazne dla osób z zaburzeniami
widzenia kolorów# a) scale_color_viridis_d() - dla zmiennych dyskretnych
ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 3) +
scale_color_viridis_d() + # domyślnie opcja "viridis"
labs(title = "Viridis - zmienna dyskretna") +
theme_minimal()
# b) scale_fill_viridis_c() - dla zmiennych ciągłych
ggplot(df, aes(x = kategoria, y = y, fill = y)) +
geom_boxplot() +
scale_fill_viridis_c(option = "plasma") + # opcje: viridis, magma, plasma, inferno, cividis
labs(title = "Viridis - zmienna ciągła (plasma)") +
theme_minimal()
# c) Różne opcje viridis
p1 <- ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 3) +
scale_color_viridis_d(option = "magma") +
labs(title = "Magma") +
theme_minimal()
p2 <- ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 3) +
scale_color_viridis_d(option = "inferno") +
labs(title = "Inferno") +
theme_minimal()
{RColorBrewer} - klasyczne palety kolorystyczne# a) Palety sekwencyjne (dla danych uporządkowanych)
ggplot(df, aes(x = kategoria, fill = kategoria)) +
geom_bar() +
scale_fill_brewer(palette = "Blues") +
labs(title = "RColorBrewer - paleta Blues (sekwencyjna)") +
theme_minimal()
# b) Palety dywergentne (dla danych z punktem centralnym)
ggplot(df, aes(x = x, y = y, color = y)) +
geom_point(size = 3) +
scale_color_distiller(palette = "RdYlBu") +
labs(title = "RColorBrewer - paleta RdYlBu (dywergentna)") +
theme_minimal()
# c) Palety jakościowe (dla kategorii)
ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 3) +
scale_color_brewer(palette = "Set2") +
labs(title = "RColorBrewer - paleta Set2 (jakościowa)") +
theme_minimal()
# Podgląd dostępnych palet
display.brewer.all()
{wesanderson} - palety inspirowane filmami Wesa
Andersona# a) Podstawowe użycie - sprawdzamy liczbę dostępnych kolorów
ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 3) +
scale_color_manual(values = wes_palette("Darjeeling1", n = 5, type = "continuous")) +
labs(title = "Wes Anderson - Darjeeling1") +
theme_minimal()
# b) Różne palety
ggplot(df, aes(x = kategoria, fill = kategoria)) +
geom_bar() +
scale_fill_manual(values = wes_palette("GrandBudapest1", n = 5, type = "continuous")) +
labs(title = "Wes Anderson - Grand Budapest Hotel") +
theme_minimal()
# c) Paleta z większą liczbą kolorów (interpolacja)
# Niektóre palety mają mniej kolorów - trzeba użyć type = "continuous" do interpolacji
ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 3) +
scale_color_manual(values = wes_palette("Zissou1", n = 5, type = "continuous")) +
labs(title = "Wes Anderson - Zissou (interpolacja)") +
theme_minimal()
Dostępne palety:
{ggsci} - palety z czasopism naukowych i science
fiction# a) Nature Publishing Group
ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 3) +
scale_color_npg() +
labs(title = "ggsci - paleta NPG (Nature)") +
theme_minimal()
# b) AAAS (Science)
ggplot(df, aes(x = kategoria, fill = kategoria)) +
geom_bar() +
scale_fill_aaas() +
labs(title = "ggsci - paleta AAAS (Science)") +
theme_minimal()
# c) Lancet
ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 3) +
scale_color_lancet() +
labs(title = "ggsci - paleta Lancet") +
theme_minimal()
# d) Star Trek
ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 3) +
scale_color_startrek() +
labs(title = "ggsci - paleta Star Trek") +
theme_minimal()
# e) Rick and Morty
ggplot(df, aes(x = kategoria, fill = kategoria)) +
geom_bar() +
scale_fill_rickandmorty() +
labs(title = "ggsci - paleta Rick and Morty") +
theme_minimal()
Inne dostępne palety:
library(patchwork)
p_viridis <- ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 2) +
scale_color_viridis_d() +
labs(title = "viridis") +
theme_minimal() +
theme(legend.position = "none")
p_brewer <- ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 2) +
scale_color_brewer(palette = "Set2") +
labs(title = "RColorBrewer") +
theme_minimal() +
theme(legend.position = "none")
p_wes <- ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 2) +
scale_color_manual(values = wes_palette("Darjeeling1", n = 5, type = "continuous")) +
labs(title = "wesanderson") +
theme_minimal() +
theme(legend.position = "none")
p_ggsci <- ggplot(df, aes(x = x, y = y, color = kategoria)) +
geom_point(size = 2) +
scale_color_npg() +
labs(title = "ggsci") +
theme_minimal() +
theme(legend.position = "none")
(p_viridis | p_brewer) / (p_wes | p_ggsci)
🌈 🎨 🎬 🔬
{viridis} - najlepsze dla:
{RColorBrewer} - najlepsze dla:
{wesanderson} - najlepsze dla:
{ggsci} - najlepsze dla: