iT邦幫忙

2025 iThome 鐵人賽

DAY 10
0

在資料視覺化中,色彩不只是美觀的裝飾,更是傳遞資訊的重要元素。合適的顏色能幫助快速辨識不同訊息內容,避免混淆,也可以強化資料的解讀。

今天要介紹的 cols4all 套件,提供了大量的色盤選擇,並內建 GUI 介面,讓我們能透過不同條件來挑選合適的顏色。例如:

  • 適合分類資料、連續資料或雙變量的色彩組合
  • 考慮色盲族群的辨識效果
  • 提供顏色對比與可讀性分析

這些功能都能幫助我們在製作圖表時,更快找出合適的顏色。


基本指令:查詢調色盤與顏色

cols4all 提供了多種方式來檢視與查詢色盤:這些指令能快速幫助我們找到合適的色盤,並視覺化呈現效果。

# 啟動互動式 GUI
c4a_gui()

# 列出所有可用色盤
c4a_palettes()

# 只看分類型色盤
c4a_palettes(type = "cat")

# 只看 RColorBrewer 系列
c4a_palettes(series = "brewer")

# 同時篩選:分類型 + RColorBrewer 系列
c4a_palettes(type = "cat", series = "brewer")

# 預覽 brewer.set3 色盤(含缺漏值顏色)
c4a_plot_hex("brewer.set3", n = 7, include.na = TRUE)


c4a_gui(): 視覺化調色盤

https://ithelp.ithome.com.tw/upload/images/20250910/20177964shyiRlCrwO.png

Overview 畫面簡介:

  1. Palette type (色盤類型)

    • Categorical(分類型)
    • Sequential(連續型)
    • Diverging(發散型)
    • Cyclic(循環型)
    • Bivariate(雙變量)
  2. Number of colors (顏色數量)

    • 用滑桿決定顯示幾個顏色,方便依需求挑選。
  3. Sort colors (排序)

    • 可以依照 原始順序 / 色相 (Hue) / 彩度 (Chroma) / 明度 (Luminance) 排列。
  4. Palette series (色盤來源)

    • 支援多個系列(brewer、carto、viridis、hcl、matplotlib、seaborn…)。
  5. Color vision (色覺模式)

    • Normal(正常)

    • Deutan(紅綠色盲)

    • Protan(另一種紅綠色盲)

    • Tritan(藍黃色盲)

      👉 可以即時檢查顏色對不同色覺使用者是否友善。

  6. 下方表格 (色盤清單)

    • Series / Name:色盤來源與名稱
    • Max number:最多顏色數
    • Colorblind-friendly:是否對色盲友善
    • Hues/Vivid/Contrast:顏色分布、對比度等評分
    • 範例顏色:顯示實際配色

📑 各個 Tab 的作用

  1. Overview
    • 總覽所有色盤的屬性與範例顏色
    • 適合快速找到候選色盤
  2. Color Blind Friendliness
    • 專門檢查色盤對不同色盲類型的辨識度
    • 幫助避免誤讀,特別重要於醫學或公共資訊圖表
  3. HCL Analysis
    • 以 Hue(色相)、Chroma(彩度)、Luminance(明度)分析顏色分布
    • 可檢查是否有顏色太相近、導致辨識困難
  4. Contrast
    • 顯示顏色在 黑色背景 / 白色背景 下的對比度
    • 保證投影片或不同螢幕上仍可讀
  5. 3D Blues
    • 三維視覺化顏色屬性,讓你更直觀理解色彩的差異
  6. Naming
    • 顯示每個顏色的名稱或編碼,方便記錄與再利用
  7. Application
    • 展示如何將色盤套用在 ggplot2 等繪圖工具中的範例

範例一:在 ggplot2 中套用色盤

library(ggplot2)
library(cols4all)

df <- data.frame(
  category = c("A", "B", "C", "D", "E"),
  value = c(23, 45, 12, 67, 34)
)

# 套用 brewer.set3 色盤
ggplot(df, aes(x = category, y = value, fill = category)) +
  geom_col() +
  scale_fill_manual(values = c4a("brewer.set3")) +
  labs(title = "使用 brewer.set3 色盤的範例")

https://ithelp.ithome.com.tw/upload/images/20250910/20177964dIF9iEnzq3.png


範例二:從調色盤中挑選自訂義顏色

# 取出 brewer.set3 的顏色
brewer_colors <- c4a("brewer.set3", n = 7)
brewer_colors

# 挑選第 1, 3, 4, 6, 7 個顏色
my_colors <- brewer_colors[c(1, 3, 4, 6, 7)]

# 繪製圖表
ggplot(df, aes(x = category, y = value, fill = category)) +
  geom_col() +
  scale_fill_manual(values = my_colors) +
  labs(title = "使用自定義色盤的範例")

https://ithelp.ithome.com.tw/upload/images/20250910/20177964BiRrE6t2sL.png


小結

今天介紹了 cols4all 的核心功能:

  1. 查詢與瀏覽色盤:透過 c4a_gui()c4a_palettes() 快速找到合適配色。
  2. 與 ggplot2 整合:用 scale_fill_manual() 套用完整色盤。
  3. 自訂顏色:從色盤中挑選特定顏色,靈活應用於圖表。

色彩是一種資訊的語言,選對色盤不只能讓圖表更美觀,也能讓數據更容易被理解。製圖時,建議可先打開 c4a_gui(),為圖形挑選合適的色彩。

🔎 English Abstract

Colors in data visualization are not just decorative but essential for conveying information. The R package cols4all offers a wide variety of palettes and a GUI tool to help users find suitable color schemes. Palettes can be filtered by type—categorical, sequential, diverging, cyclic, or bivariate—and adjusted by the number of colors. Users can also sort palettes by hue, chroma, or luminance, and check accessibility for people with color vision deficiency. Each palette includes details such as maximum colors, contrast, and whether it is colorblind-friendly.

Beyond the overview, the GUI provides specialized tabs: Color Blind Friendliness for accessibility checks, HCL Analysis for color structure, Contrast for background testing, 3D Blues for visualizing color attributes, Naming for identifying colors, and Application for examples in ggplot2. Palettes can be applied directly using scale_fill_manual() or customized by selecting specific colors. With cols4all, users can make charts that are visually appealing, accessible, and effective in communicating data insights.


上一篇
修飾與加工
下一篇
故事與重點:資料視覺化的根本
系列文
資料視覺化的探索之旅:從 ggplot2 技術到視覺化設計11
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言