開始使用免費開始

發散色階範例

來看看另一個預設色階不合適的資料集。這個柵格 migration,記錄 1990 到 2000 年間,每個柵格格點的淨遷入人數估計值。正數代表淨移入,負數代表移出。先看一下:

tm_shape(migration) +
  tm_raster() +
  tm_legend(outside = TRUE, 
            outside.position = c("bottom"))

預設色階看起來不是很有幫助,不過 tmap 實際上做了件聰明的事:它自動選用了發散色階。由於人口的大幅移動可能是很大的正值「或」很大的負值(就絕對值而言),因此發散色階是合適的。0(也就是沒有淨遷徙)是很自然的中點。

tmap 會在變數同時含有正值與負值時選擇發散色階,並以 0 為中點。但這並非總是最合適的作法。想像你要繪製百分比形式的相對變化;此時 100% 可能才是最直覺的中點。若你需要不同的作法,最佳方式是自己產生一組發散調色盤(步數用奇數,這樣才有正中央的顏色),並自行指定分段(breaks)。

現在就試著自己加入發散色階,做出更具資訊性的地圖吧。

(資料來源:de Sherbinin, A., M. Levy, S. Adamo, K. MacManus, G. Yetman, V. Mara, L. Razafindrazay, B. Goodrich, T. Srebotnjak, C. Aichele, and L. Pistolesi. 2015. Global Estimated Net Migration Grids by Decade: 1970-2000. Palisades, NY: NASA Socioeconomic Data and Applications Center (SEDAC). http://dx.doi.org/10.7927/H4319SVC Accessed 27 Sep 2016)

本練習屬於課程

在 R 視覺化地理空間資料

檢視課程

練習說明

  • 列印 migration,確認它是 RasterLayer 物件,並查看遷徙值的範圍。
  • 從 RColorBrewer 的 "RdGy" 調色盤產生 7 色的發散色盤,命名為 red_gray。
  • 在你的圖中使用這組發散色彩 red_gray 作為調色盤。『這會使用你的顏色,但分段仍不理想。』
  • 為色階加入固定分段:c(-5e6, -5e3, -5e2, -5e1, 5e1, 5e2, 5e3, 5e6)

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Print migration


# Diverging "RdGy" palette
red_gray <- brewer.pal()

# Use red_gray as the palette 
tm_shape(migration) +
  tm_raster() +
  tm_legend(outside = TRUE, outside.position = c("bottom"))

# Add fixed breaks 
tm_shape(migration) +
  tm_raster() +
  tm_legend(outside = TRUE, outside.position = c("bottom"))
編輯並執行程式碼