29

ggplot2 配色

色彩方案与调色板

Tidyverse 篇
R
library(tidyverse)
library(palmerpenguins)

为了让图更好看,需要在画图中使用配色,但如果从颜色的色相、色度、明亮度三个属性(Hue-Chroma-Luminance )开始学,感觉这样要学的东西太多了 [R表达式]. 事实上,大神们已经为我们准备好了很多好看的模板,我们可以偷懒直接拿来用[R表达式].

我个人比较喜欢colorspace中的配色,今天我们就讲讲如何使用这个宏包!

R
library(colorspace)

colorspace 宏包提供了三种类型的配色模板:

图片
  • Qualitative: 分类,用于呈现分类信息,比如不同种类用不同的颜色,颜色之间一般对比鲜明。
  • Sequential: 序列,用于呈现有序/连续的数值信息,比如为了展示某地区黑人比例,比例越高颜色越深,比例越低颜色越浅。
  • Diverging: 分歧,用于呈现有序/连续的数值信息,这些数值围绕着一个中心值,比中心值越大的方向用一种渐变色,比中心值越小用另一种渐变色。

三种类型对应着三个函数 qualitative_hcl(), sequential_hcl(), 和 diverging_hcl().

配色模板

根据你需要颜色的三大种类,先找适合的模板palettes

R
hcl_palettes(plot = TRUE)

或者指定某个种类,显示所有的模板

R
hcl_palettes("qualitative", plot = TRUE)
hcl_palettes("sequential (single-hue)", n = 7, plot = TRUE)
hcl_palettes("sequential (multi-hue)", n = 7, plot = TRUE)
hcl_palettes("diverging", n = 7, plot = TRUE)

如果看中某个模板的颜色,可以获取它的值,比如sequential种类下的 "Peach"模板,

R
#colorspace::diverging_hcl(n = 7, "Dark 2")
#colorspace::qualitative_hcl(4, palette = "myset")
colorspace::sequential_hcl(n = 7, palette = "Peach")

当然,在用之前,我们先检验下

R
colorspace::sequential_hcl(n = 7, palette = "Peach") %>% 
  colorspace::swatchplot()

在ggplot2中使用

在ggplot2中可以免去手工操作,而直接使用。事实上, colorspace 模板使用起来很方便,有统一格式scale_<aesthetic>_<datatype>_<colorscale>(),

  • 这里 <aesthetic> 是指定映射 (fill, color, colour),
  • 这里 <datatype> 是表明数据变量的类型 (discrete, continuous, binned),
  • 这里 colorscale 是声明颜色标度类型 (qualitative, sequential, diverging, divergingx).
Scale functionAestheticData typePalette type
scale_color_discrete_qualitative()colordiscretequalitative
scale_fill_continuous_sequential()fillcontinuoussequential
scale_colour_continuous_divergingx()colourcontinuousdiverging

使用案例1

ggplot2默认

R
penguins %>% 
   ggplot(aes(bill_length_mm, fill = species)) +
   geom_density(alpha = 0.6)

手动修改

R
penguins %>% 
   ggplot(aes(bill_length_mm, fill = species)) +
   geom_density(alpha = 0.6) +
   scale_fill_manual(
     breaks = c("Adelie", "Chinstrap", "Gentoo"), 
     values = c("darkorange", "purple", "cyan4")
  )

使用colorspace模板配色

R
penguins %>% 
   ggplot(aes(bill_length_mm, fill = species)) +
   geom_density(alpha = 0.6) +
   scale_fill_discrete_qualitative(palette = "cold")
R
penguins %>% 
   ggplot(aes(bill_length_mm, fill = species)) +
   geom_density(alpha = 0.6) +
   scale_fill_discrete_qualitative(palette = "cold", nmax = 4, order = 2:4)
R
sequential_hcl(palette = "Purples 3", n = 6) %>% 
  swatchplot()

penguins %>% 
   ggplot(aes(bill_length_mm, fill = species)) +
   geom_density(alpha = 0.6) +
   scale_fill_discrete_sequential(palette = "Purples 3", 
                                  nmax = 6, 
                                  rev = FALSE, 
                                  order = 3:5)

使用案例2

R
temps_months <- read_csv("./demo_data/tempnormals.csv") %>%
  group_by(location, month_name) %>%
  summarize(mean = mean(temperature)) %>%
  mutate(
    month = factor(
      month_name,
      levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    ),
    location = factor(
      location, levels = c("Death Valley", "Houston", "San Diego", "Chicago")
    )
  ) %>%
  select(-month_name)

temps_months
R
temps_months %>% 
  ggplot(aes(x = month, y = location, fill = mean)) +
  geom_tile(width = 0.95, height = 0.95) +
  coord_fixed(expand = FALSE) +
  theme_classic()
R
temps_months %>% 
  ggplot(aes(x = month, y = location, fill = mean)) +
  geom_tile(width = 0.95, height = 0.95) +
  coord_fixed(expand = FALSE) +
  theme_classic() +
  scale_fill_gradient()
R
temps_months %>% 
  ggplot(aes(x = month, y = location, fill = mean)) +
  geom_tile(width = 0.95, height = 0.95) +
  coord_fixed(expand = FALSE) +
  theme_classic() +
  scale_fill_viridis_c()
R
temps_months %>% 
  ggplot(aes(x = month, y = location, fill = mean)) +
  geom_tile(width = 0.95, height = 0.95) +
  coord_fixed(expand = FALSE) +
  theme_classic() +
  scale_fill_viridis_c(option = "B", begin = 0.15)
R
temps_months %>% 
  ggplot(aes(x = month, y = location, fill = mean)) +
  geom_tile(width = 0.95, height = 0.95) +
  coord_fixed(expand = FALSE) +
  theme_classic() +
  scale_fill_continuous_sequential(palette = "YlGnBu", rev = FALSE)
R
temps_months %>% 
  ggplot(aes(x = month, y = location, fill = mean)) +
  geom_tile(width = 0.95, height = 0.95) +
  coord_fixed(expand = FALSE) +
  theme_classic() +
  scale_fill_continuous_sequential(palette = "Viridis", rev = FALSE)
R
temps_months %>% 
  ggplot( aes(x = month, y = location, fill = mean)) +
  geom_tile(width = 0.95, height = 0.95) +
  coord_fixed(expand = FALSE) +
  theme_classic() +
  scale_fill_continuous_sequential(palette = "Inferno", begin = 0.15, rev = FALSE)
R
temps_months %>% 
  ggplot(aes(x = month, y = location, fill = mean)) +
  geom_tile(width = 0.95, height = 0.95) +
  coord_fixed(expand = FALSE) +
  theme_classic() +
  scale_fill_continuous_sequential(palette = "Plasma", begin = 0.35, rev = FALSE)

配色

这里有个小小的提示:

  • 尽可能避免使用"red", "green", "blue", "cyan", "magenta", "yellow"颜色
  • 使用相对柔和的颜色"firebrick", "springgreen4", "blue3", "turquoise3", "darkorchid2", "gold2",会让人觉得舒服

可以对比下

R
colorspace::swatchplot(c("red", "green", "blue", "cyan", "magenta", "yellow"))
colorspace::swatchplot(c("firebrick", "springgreen4", "blue3", "turquoise3", "darkorchid2", "gold2"))

color-wheel