天天看點

繪圖技巧 | 三元相分級統計地圖(Ternary Choropleth Map)可視化繪制

之前有小夥伴在讨論群裡提問關于分級統計地圖(choropleth maps) 的繪制方法,剛開始看到這個問題的時候覺得比較簡單,就給出了幾個處理方法,有R的也有基于Python 的,但後來和提問小夥伴一聊,才知道是要繪制一個有 ”三元相映射圖例的” 分級統計地圖。之前也答應會出一期類似的推文,中間有太多的事情要做,導緻拖得有點久。作為過完年的第一篇原創推文,本期我們就使用可視化功能強大的R來繪制此類地圖,主要涉及内容如下:

  • R-tricolore包簡介
  • R-tricolore包實踐

R-tricolore包簡介

在得知類似需求後,我就根據類似需求進行資料查詢,在經過多次查找之後,就找到了R-tricolore包可以較好的完成類似繪制需求(Python的目前還沒找到繪制方法),介紹如下:

1. 官網網址

R-tricolore包的官方網址為:https://github.com/jschoeley/tricolore ,小夥伴們想了解更多的内容可浏覽該網址查閱哈。

2. 主要功能

R-tricolore 包可為三元相圖的組成成分提供靈活的可視化色标,其主要功能是将任何三元合成顔色編碼為三種原色的混合,并繪制合适的顔色鍵。主要提供以下可視化顔色設定:

  • 離散(discrete)和連續(continuous)色彩支援,
  • 通過居中支援不平衡的成分資料(unbalanced compositional data),
  • 通過縮放支援範圍非常窄的資料(data with very narrow range),
  • 色調,色度和亮度選項設定。

3. R-tricolore 包樣例

R-tricolore 包提供了豐富的樣例供學習參考,接下來,我将列舉幾個例子友善大家了解:

  • 基本三元圖繪制

代碼:

library(tricolore)

# 生成模拟資料
P <- as.data.frame(prop.table(matrix(runif(3^6), ncol = 3), 1))
# 使用Tricolore生成需要的資料:該步驟最為重要
colors_and_legend <- Tricolore(P, 'V1', 'V2', 'V3')
# 展示生成的資料(部分)
head(colors_and_legend$rgb)
#結果如下
"#A37D7D" "#7A86A1" "#AF9B47" "#6E8E72" "#00AFAE" "#727272"
           

複制

而使用一下代碼就可以直接繪制三元相圖:

colors_and_legend$key
           

複制

結果如下:

繪圖技巧 | 三元相分級統計地圖(Ternary Choropleth Map)可視化繪制
  • 三元分級統計地圖

繪制完tricolore包主要的繪圖方法(用于定制化繪制三元相圖),接下來我們看下官網提供的地圖映射繪制方法(主要介紹的内容):

「樣例一:」

# color-code the data set and generate a color-key
tric_educ <- Tricolore(euro_example,
                       p1 = 'ed_0to2', p2 = 'ed_3to4', p3 = 'ed_5to8')
# add the vector of colors to the `euro_example` data
euro_example$educ_rgb <- tric_educ$rgb
library(ggplot2)
library(ggtern)

plot_educ <-
  # using data sf data `euro_example`...
  ggplot(euro_example) +
  # ...draw a choropleth map
  geom_sf(aes(fill = educ_rgb, geometry = geometry), size = 0.1) +
  # ...and color each region according to the color-code
  # in the variable `educ_rgb`
  scale_fill_identity()

plot_educ <-
  plot_educ +
  annotation_custom(
    ggplotGrob(
      tric_educ$key +
        labs(L = '0-2', T = '3-4', R = '5-8')),
    xmin = 55e5, xmax = 75e5, ymin = 8e5, ymax = 80e5
  ) +
theme_void() +
  coord_sf(datum = NA) +
  labs(title = 'European inequalities in educational attainment',
       subtitle = 'Regional distribution of ISCED education levels for people aged 25-64 in 2016.')
           

複制

可視化結果如下:

繪圖技巧 | 三元相分級統計地圖(Ternary Choropleth Map)可視化繪制

「樣例二:breaks = 2」

# color-code the data set and generate a color-key
tric_educ_disc <- Tricolore(euro_example,
                            p1 = 'ed_0to2', p2 = 'ed_3to4', p3 = 'ed_5to8',
                            breaks = 2)
euro_example$educ_rgb_disc <- tric_educ_disc$rgb

ggplot(euro_example) +
  geom_sf(aes(fill = educ_rgb_disc, geometry = geometry), size = 0.1) +
  scale_fill_identity() +
  annotation_custom(
    ggplotGrob(
      tric_educ_disc$key +
        labs(L = '0-2', T = '3-4', R = '5-8')),
    xmin = 55e5, xmax = 75e5, ymin = 8e5, ymax = 80e5
  ) +
  theme_void() +
  coord_sf(datum = NA) +
  labs(title = 'European inequalities in educational attainment',
       subtitle = 'Regional distribution of ISCED education levels for people aged 25-64 in 2016.')
           

複制

可視化結果如下:

繪圖技巧 | 三元相分級統計地圖(Ternary Choropleth Map)可視化繪制

「樣例三:Ternary centering」

tric_lf_non_centered <- Tricolore(euro_example, breaks = Inf,
                                  'lf_pri', 'lf_sec', 'lf_ter')

euro_example$rgb_lf_non_centered <- tric_lf_non_centered$rgb

ggplot(euro_example) +
  geom_sf(aes(fill = rgb_lf_non_centered, geometry = geometry), size = 0.1) +
  scale_fill_identity() +
  annotation_custom(
    ggplotGrob(tric_lf_non_centered$key +
                 labs(L = '% Primary', T = '% Secondary', R = '% Tertiary')),
    xmin = 55e5, xmax = 75e5, ymin = 8e5, ymax = 80e5
  ) +
  theme_void() +
  coord_sf(datum = NA) +
  labs(title = 'European inequalities in labor force composition',
       subtitle = 'Regional distribution of labor force across the three sectors in 2016.')
           

複制

可視化結果如下:

繪圖技巧 | 三元相分級統計地圖(Ternary Choropleth Map)可視化繪制

當然通過設定還可以繪制如下可視化效果:

繪圖技巧 | 三元相分級統計地圖(Ternary Choropleth Map)可視化繪制

R-tricolore包實踐

由于上述介紹的都是官網的例子,這部分我們使用新的資料進行這種 “三元分級統計地圖” 的繪制,詳細内容如下(資料和相關代碼之前的推文繪圖技巧 | 雙變量映射地圖可視化繪制方法 類似,這裡直接給出相應的代碼:

  • 地圖資料可視化

代碼:

library(sf)
library(tidyverse)
library(hrbrthemes)
library(tricolore)

us_data <- socviz::county_data %>% select(id,fips,name,state, land_area,hh_income,pop)
tric_usdata <- Tricolore(us_data,
                       p1 = 'land_area', p2 = 'pop', p3 = 'hh_income',breaks = 3)
#添加新資料
us_data$us_rgb <- tric_usdata$rgb               

country_sf <- albersusa::counties_sf()
country_sf <- mutate(country_sf,fips=as.character(fips)) #mutate()生成新列
tri_data <- left_join(country_sf,us_data,by = c("fips"="id"))
us_map2 <- ggplot(data = process_data_2163) +
  geom_sf(aes(fill=us_rgb,geometry = geometry),size=.1) +
  scale_fill_identity() # 該步驟確定每個區域之為對應的顔色值

           

複制

可視化結果如下:

繪圖技巧 | 三元相分級統計地圖(Ternary Choropleth Map)可視化繪制
  • 添加三元相圖及美化
final_map <-
  us_map2 +
  annotation_custom(
      ggplotGrob(tric_usdata$key+
                theme(text = element_text(size = 8),
                      plot.background = element_rect(fill = "#F0E5DE",colour = "#F0E5DE"))),
      xmin = 1478654 , xmax = 568885.4 , ymin = -2642789, ymax = -1753061
      
  ) +
  labs(
      title = "Example Ternary Choropleth Map of <span style='color:#D20F26'>USA</span>",
      subtitle = "Made in <span style='color:#1A73E8'>tricolore</span>",
      caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>" 
  ) +
 theme_void(base_family = "Roboto Condensed") +
    theme(
           plot.background = element_rect(fill = "#F0E5DE",colour = "#F0E5DE"),
           plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                     size = 24, margin = margin(t = 1, b = 12)),
           plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
           plot.caption = element_markdown(face = 'bold',size = 12,hjust = 1)
      )
           

複制

最終的可視化結果如下:

繪圖技巧 | 三元相分級統計地圖(Ternary Choropleth Map)可視化繪制

總結

作為過完年的第一篇原創推文還是以小編喜歡的空間可視化作品開始,希望小夥伴們可以從中擷取繪圖靈感,大家也可以結合繪圖技巧 | 雙變量映射地圖可視化繪制方法 這篇推文進行對比繪制。