區間刻度的範例
回到你先前繪製的 18 至 24 歲人口比例地圖:
tm_shape(prop_by_age) +
tm_raster("age_18_24", palette = vir) +
tm_legend(position = c("right", "bottom"))
這張圖的問題在於:大多數比例都落在最低的顏色等級,因此你在圖上看不到太多細節。解法之一是:不要把變數的範圍切成等長的區間,而是切成更有意義的分類。
先從重現 tmap 的預設分箱開始:5 個類別,使用 "pretty" 的斷點。接著你可以試試其他把變數切成區間的方法。直接使用 classIntervals() 能快速看見斷點會落在哪裡,但要評估一組斷點實不實用,最好的方式還是把它畫出來。
(順帶一提,另一種解法是對變數做轉換,讓在轉換後尺度上的等長區間更有用。)
本練習屬於課程
在 R 視覺化地理空間資料
練習說明
- 對
values(prop_by_age[["age_18_24"]])呼叫classIntervals(),設定n = 5與style = "pretty"。*看出問題了嗎?共有 130,770 個網格細胞落在第一個區間。* - 仍以相同資料呼叫
classIntervals(),但把style改成"quantile"。 - 在你的圖中,將
n與style參數傳入tm_raster()圖層,以使用等大小的分箱。 - 為
values(prop_by_age[["age_18_24"]])製作直方圖。*你會把斷點設在哪裡?* - 在
tm_raster()中自訂斷點:指定breaks = c(0.025, 0.05, 0.1, 0.2, 0.25, 0.3, 1)。 - 使用
tmap_save(),以filename"prop_18-24.html"將最後的地圖儲存為 leaflet 圖層。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
mag <- viridisLite::magma(7)
library(classInt)
# Create 5 "pretty" breaks with classIntervals()
# Create 5 "quantile" breaks with classIntervals()
# Use 5 "quantile" breaks in tm_raster()
tm_shape(prop_by_age) +
tm_raster("age_18_24", palette = mag) +
tm_legend(position = c("right", "bottom"))
# Create histogram of proportions
# Use fixed breaks in tm_raster()
tm_shape(prop_by_age) +
tm_raster("age_18_24", palette = mag,
style = "fixed")
# Save your plot to "prop_18-24.html"