开始使用免费开始使用

来做一次对数变换

让我们查看北卡罗来纳州(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))
编辑并运行代码