來做一下對數轉換
先看看北卡羅來納州(NC)郵遞區號平均收入的四分位數。
summary(high_inc$mean_income)
Min. 1st Qu. Median Mean 3rd Qu. Max.
55947 62380 71655 83682 90695 550849
範圍接近 $500,000!但其中位數比起最大值更接近最小值,表示分配右偏。因為平均收入這個變數含有特別大的數值,連續色帶就不太好用。對右偏的變數做對數轉換,能把大數值拉近平均值,讓變數的分配更接近對稱。
在地圖上對平均收入做對數轉換後,高收入郵遞區號之間的色帶差異會更明顯,也能更清楚地呈現全州平均收入的分布。
本練習屬於課程
在 R 中使用 leaflet 製作互動式地圖
練習說明
- 建立一個以對數後數值為基礎的 nc_pal 調色盤版本。
- 將這個對數調色盤套用到
leaflet地圖。 - 將
leaflet地圖中的底圖圖磚註解掉,以便更容易觀察平均收入的差異(在加入地圖圖磚的那個函式前加上不含空白的 #)。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Use the log function to create a new version of nc_pal
nc_pal <- colorNumeric("YlGn", domain = ___(high_inc@data$mean_income))
# comment out the map tile
high_inc %>%
leaflet() %>%
___addProviderTiles("CartoDB") %>%
# apply the new nc_pal to the map
addPolygons(weight = 1, color = ~nc_pal(___(mean_income)), fillOpacity = 1,
label = ~paste0("Mean Income: ", dollar(mean_income)),
highlightOptions = highlightOptions(weight = 5, color = "white", bringToFront = TRUE))