3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

khromaパッケージのカラースキームを解説する

Posted at

khromaパッケージ

khromaパッケージは学術的な成果を全ての人が享受するために注意深く選ばれたカラースキームを提供します。Paul Tolや岡部・伊藤が提供するカラースキームを実装したものとなっています。

# パッケージ読み込み
library(tidyverse)
library(khroma)

# データ準備
data <- iris %>%
  as_tibble() %>%
  mutate(SepLenCut =case_when( 
                     Sepal.Length < 5 ~ "5cm未満" ,
                     Sepal.Length >= 5 & Sepal.Length < 6 ~ "6cm未満" ,
                     Sepal.Length >= 6 & Sepal.Length < 7 ~ "7cm未満" ,
                     Sepal.Length >= 7 ~ "7cm以上" ,
                     )) 
data$SepLenCut <- fct_relevel(data$SepLenCut , 
                              "5cm未満", "6cm未満" , "7cm未満" ,"7cm以上")
data
# A tibble: 150 x 6
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species SepLenCut
          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <fct>    
 1          5.1         3.5          1.4         0.2 setosa  6cm未満  
 2          4.9         3            1.4         0.2 setosa  5cm未満  
 3          4.7         3.2          1.3         0.2 setosa  5cm未満  
 4          4.6         3.1          1.5         0.2 setosa  5cm未満  
 5          5           3.6          1.4         0.2 setosa  6cm未満  
 6          5.4         3.9          1.7         0.4 setosa  6cm未満  
 7          4.6         3.4          1.4         0.3 setosa  5cm未満  
 8          5           3.4          1.5         0.2 setosa  6cm未満  
 9          4.4         2.9          1.4         0.2 setosa  5cm未満  
10          4.9         3.1          1.5         0.1 setosa  5cm未満  
# … with 140 more rows

ggplotデフォルト

# 構成比棒グラフ
data %>%
  ggplot() +
  geom_bar(aes(x = SepLenCut , fill = Species) ,
           position = "fill") +
  coord_flip()
# 散布図(離散量)
data %>%
  ggplot() +
  geom_point(aes(x = Sepal.Length , y = Petal.Length , colour = Species))
# 散布図(連続量)
data %>%
  ggplot() +
  geom_point(aes(x = Sepal.Length , y = Petal.Length , colour = Sepal.Width) ,
  size = 5)

01.png

scale_okabeito_discrete

岡部・伊藤の提唱による離散量向けカラーパレットです。
色覚研究HP(伊藤)
バリアフリープレゼンテーション法(岡部)

見間違うことのない隔絶したカラースキームです。散布図点群の視認性も良いでしょう。

# 構成比棒グラフ
data %>%
  ggplot() +
  geom_bar(aes(x = SepLenCut , fill = Species) ,
           position = "fill" ) +
  coord_flip()+
  scale_fill_okabeito()
# 散布図(離散量)
data %>%
  ggplot() +
  geom_point(aes(x = Sepal.Length , y = Petal.Length , colour = Species)) +
  scale_colour_okabeito()

02.png

Paul Tolのカラースキーム

Paul Tolのカラースキームはscale_ptol()でも提供されていますが、khromaではさらに多くのパレットが用意されています。

scale_colour_bright

Tol氏のドキュメント(Colour Schemes)で「定性データのためのデフォルトの配色」とされるもので、Tolの離散量(定性的)カラースキームの基本となるものです。

# 構成比棒グラフ
data %>%
  ggplot() +
  geom_bar(aes(x = SepLenCut , fill = Species) ,
           position = "fill" ) +
  coord_flip()+
  scale_fill_bright()
# 散布図(離散量)
data %>%
  ggplot() +
  geom_point(aes(x = Sepal.Length , y = Petal.Length , colour = Species)) +
  scale_colour_bright()

03.png

scale_colour_contrast

brightよりも色の違いが強調されます。前出のTol氏のドキュメントで「少ない色で十分な場合の代替案」とされます。このパレットはグレースケールに変換しても機能し、モノクロ出力しても十分に実用に耐えるものです。

# 構成比棒グラフ
data %>%
  ggplot() +
  geom_bar(aes(x = SepLenCut , fill = Species) ,
           position = "fill" ) +
  coord_flip()+
  scale_fill_contrast()
# 散布図(離散量)
data %>%
  ggplot() +
  geom_point(aes(x = Sepal.Length , y = Petal.Length , colour = Species)) +
  scale_colour_contrast()

04.png

scale_colour_vibrant

TensorBoard用のデータビジュアライズ向けにデザインされたカラーパレット。TensorBoardの特徴的なオレンジは印刷向けのオレンジ色に改良されています。普段遣いしやすいカラースキームです。

# 構成比棒グラフ
data %>%
  ggplot() +
  geom_bar(aes(x = SepLenCut , fill = Species) ,
           position = "fill" ) +
  coord_flip()+
  scale_fill_vibrant()
# 散布図(離散量)
data %>%
  ggplot() +
  geom_point(aes(x = Sepal.Length , y = Petal.Length , colour = Species)) +
  scale_colour_vibrant()

05.png

scale_colour_muted

scale_colour_brightの色数を増やしたバージョンです。brightの7色に対して、9色まで対応しています。

# 構成比棒グラフ
data %>%
  ggplot() +
  geom_bar(aes(x = SepLenCut , fill = Species) ,
           position = "fill" ) +
  coord_flip()+
  scale_fill_muted()
# 散布図(離散量)
data %>%
  ggplot() +
  geom_point(aes(x = Sepal.Length , y = Petal.Length , colour = Species)) +
  scale_colour_muted()

06.png

scale_colour_pale

明度の高いパレット。背景色として使用し、上にテキストが載る用例を想定しています。colourでは特性を活かせず、fill向きと言えます。

# 構成比棒グラフ
data %>%
  count(SepLenCut , Species) %>%
  ggplot(aes(x = SepLenCut , y = n , fill = Species , label = Species) ) +
  geom_bar( stat = "identity" , position = "fill") +
  geom_text_repel(position = "fill" , hjust = 1.5) +
  coord_flip()+
  scale_fill_pale() 
# 散布図(離散量)
data %>%
  ggplot() +
  geom_point(aes(x = Sepal.Length , y = Petal.Length , colour = Species)) +
  scale_colour_pale()

07.png

scale_colour_dark

paleとは逆に白文字をテキストとして載せるのに適しています。pale同様、colourでは特性を活かせないため、fill向きと言えます。

# 構成比棒グラフ
data %>%
  count(SepLenCut , Species) %>%
  ggplot(aes(x = SepLenCut , y = n , fill = Species , label = Species) ) +
  geom_bar( stat = "identity" , position = "fill") +
  geom_text_repel(position = "fill" , hjust = 1.5 , colour = "white") +
  coord_flip()+
  scale_fill_dark() 
# 散布図(離散量)
data %>%
  ggplot() +
  geom_point(aes(x = Sepal.Length , y = Petal.Length , colour = Species)) +
  scale_colour_dark()

08.png

scale_colour_light

brightをベースにより淡い色で構成されるパレット。paleよりも明瞭な色で構成されていますが、上に載った黒いテキストがはっきりと視認できるように設計されています。

# 構成比棒グラフ
data %>%
  count(SepLenCut , Species) %>%
  ggplot(aes(x = SepLenCut , y = n , fill = Species , label = Species) ) +
  geom_bar( stat = "identity" , position = "fill") +
  geom_text_repel(position = "fill" , hjust = 1.5 ) +
  coord_flip()+
  scale_fill_light()
# 散布図(離散量)
data %>%
  ggplot() +
  geom_point(aes(x = Sepal.Length , y = Petal.Length , colour = Species)) +
  scale_colour_light()

09.png

ダイバースカラー

0を中位点としてプラスとマイナスがある連続量のためのカラーパレットです。

data_div <- data %>%
  mutate(trans.Sepal.Width = Sepal.Width - median(data$Sepal.Width))

scale_colour_sunset

青→赤に遷移するパレット。ColorBrewer のRdYluと関連のあるパレットですが、中央の色が暗く設計されています。

# 散布図(連続量)
data_div %>%
  ggplot(aes(x = Sepal.Length , 
             y = Petal.Length , 
             colour = trans.Sepal.Width ) ) +
  geom_point(size = 5) +
  scale_colour_sunset()

10.png

scale_colour_BuRd

青→赤に遷移するColorBrewer RdBuのリバースバージョンです。

# 散布図(連続量)
data_div %>%
  ggplot(aes(x = Sepal.Length , 
             y = Petal.Length , 
             colour = trans.Sepal.Width )) +
  geom_point(size = 5) +
  scale_colour_BuRd()

11.png

scale_colour_PRGn

紫→緑に遷移するColorBrewer RRGndをベースとしたカラーパレットです。グリーンの一部をプリントに適したカラーに改変しています。

# 散布図(連続量)
data_div %>%
  ggplot(aes(x = Sepal.Length , 
             y = Petal.Length , 
             colour = trans.Sepal.Width )) +
  geom_point(size = 5) +
  scale_colour_PRGn()

12.png

シーケンシャルカラー

一般的な連続量のためのカラーパレット。

scale_colour_YlOrBr

白→黄色→茶色と遷移するカラーパレットです、ColorBrewerのYlOrBrスキームをベースに設計されています。

# 散布図(連続量)
data %>%
  ggplot(aes(x = Sepal.Length , 
             y = Petal.Length , 
             colour = Sepal.Width )) +
  geom_point(size = 5) +
  scale_colour_YlOrBr()

14.png

scale_colour_iridescent

黄色から緑、青を経由して紫へと遷移するカラーパレット。

# 散布図(連続量)
data %>%
  ggplot(aes(x = Sepal.Length , 
             y = Petal.Length , 
             colour = Sepal.Width )) +
  geom_point(size =5) +
  scale_colour_iridescent()

scale_colour_discreterainbow

15.png

離散量用のパレットです。地図のために開発されたカラースキームで彩度が非常に高いことが特徴です。

# 散布図(連続量)
data %>%
  ggplot(aes(x = Sepal.Length , 
             y = Petal.Length , 
             colour = Species )) +
  geom_point() +
  scale_colour_discreterainbow()
# 構成比棒グラフ
data %>%
  ggplot() +
  geom_bar(aes(x = SepLenCut , fill = Species) ,
           position = "fill" ) +
  coord_flip()+
  scale_fill_discreterainbow()

17.png

scale_colour_smoothrainbow

連続量・離散量両用のパレットです。discreterainbowの中間色を補完し、色数を増加したものです。

# 散布図(離散量)
data %>%
  ggplot(aes(x = Sepal.Length , 
             y = Petal.Length , 
             colour = Species )) +
  geom_point() +
  scale_colour_smoothrainbow(discrete = TRUE)
# 散布図(連続量)
data %>%
  ggplot(aes(x = Sepal.Length , 
             y = Petal.Length , 
             colour = Sepal.Width )) +
  geom_point(size = 5) +
  scale_colour_smoothrainbow(discrete = FALSE)
# 構成比棒グラフ
data %>%
  ggplot() +
  geom_bar(aes(x = SepLenCut , fill = Species) ,
           position = "fill" ) +
  coord_flip()+
  scale_fill_smoothrainbow(discrete = TRUE)

18.png

国際的な土壌分類等のカラーパレット

地図での使用が想定されています。カラーユニバーサル性は保証されていません。

scale_fill_land

土地分類のためのカラーパレット。

land <- data.frame(
name = c(
"water", "evergreen needleleaf forest", "deciduous needleleaf forest",
"mixed forest", "evergreen broadleaf forest", "deciduous broadleaf forest",
"woodland", "wooded grassland", "grassland", "cropland", "closed shrubland",
"open shrubland", "bare ground", "urban and built"
)
)
ggplot2::ggplot(land, ggplot2::aes(fill = name)) +
ggplot2::geom_rect(aes(xmin = rep(0, 14), xmax = rep(1, 14),
ymin = 1:14, ymax = 1:14+1)) +
ggplot2::scale_y_reverse() +
scale_fill_land(name = "land")

19.png

scale_fill_soil

土壌図のためのカラーパレット。

soil <- data.frame(
name = c(
"Acrisol", "Albeluvisol", "Andosol", "Anthrosol", "Arenosol", "Calcisol",
"Cambisol", "Chernozem", "Cryosol", "Fluvisol", "Kastanozem", "Gleysol",
"Gypsisol", "Histosol", "Leptosol", "Luvisol", "Phaeozem", "Planosol",
"Podzol", "Regosol", "Solonchak", "Solonetz", "Umbrisol", "Vertisol"
)
)
ggplot2::ggplot(soil, ggplot2::aes(fill = name)) +
ggplot2::geom_rect(aes(xmin = rep(0, 24), xmax = rep(1, 24),
ymin = 1:24, ymax = 1:24+1)) +
ggplot2::scale_y_reverse() +
scale_fill_soil(name = "Soil")

20.png

scale_fill_stratigraphy

地質年代のためのカラーパレット。

strati <- data.frame(
name = c("Phanerozoic", "Paleozoic", "Cambrian", "Ordovician", "Silurian",
"Devonian", "Carboniferous", "Mesozoic", "Triassic", "Cretaceous",
"Jurassic", "Cenozoic", "Paleogene", "Neogene", "Quaternary"),
type = c("Eon", "Era", "Period", "Period", "Period", "Period", "Period",
"Era", "Period", "Period", "Period", "Era", "Period", "Period",
"Period"),
start = c(541, 541, 541, 485, 444, 419, 359,
252, 252, 201, 145, 66, 66, 23, 2.6),
end = c(0, 252, 485, 444, 419, 359, 252,
66, 201, 145, 66, 2.6, 23, 2.6, 0)
)
ggplot2::ggplot(strati, ggplot2::aes(fill = name)) +
ggplot2::geom_rect(aes(xmin = rep(0, 15), xmax = rep(1, 15),
ymin = start, ymax = end)) +
ggplot2::scale_y_reverse() +
ggplot2::facet_grid(. ~ type) +
scale_fill_stratigraphy(name = "Stratigraphy")

22.png

まとめ

Khromaパッケージのカラースキームはオーソドックスな配色が多く、特に離散量用のパレットは用途に応じて使い分けができるため、あらゆる場面で利用することができます。中でもvibrant、brihgt、contrastはcolour、fillいずれの使用にも適する優れたカラースキームです。筆者は特にvibrantを愛用しています。

地図以外のデータ可視化では、連続量のカラーパレットを使用することは少ないのですが、順序変数(年代区分や法量区分)を使用する際には連続量的なカラーパレットを離散量データにも適用したくなります。そのような場合に適したパレットは本パッケージでは提供されていないため、筆者はviridisバッケージのscale_colour_viridis_dを主に使用しています。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?