我有一个包含县级信息的数据集,其中美国的每个县都有2个关联的列:urban_rural_type
包含县的数字分类,urban_rural_type_desc
包含数字类别的描述在前一个变量中。
我目前已成功使用以下代码将其添加到地图中:
pal <- colorFactor("viridis", domain = healthMapData()@data$urban_rural_type, reverse = T)
leafletProxy("map", data = healthMapData()) %>%
clearMarkers() %>%
clearMarkerClusters() %>%
clearShapes() %>%
addPolygons(
stroke = TRUE,
color = "white",
highlight = highlightOptions(
weight = 2,
fillOpacity = 0.6,
color = "#666",
opacity = 0.8,
bringToFront = TRUE,
sendToBack = TRUE),
opacity = 1,
weight = 0.5,
smoothFactor = 0.2,
fillOpacity = 0.8,
fillColor = pal(healthMapData()@data$urban_rural_type),
label = lapply(countyInfo, HTML)) %>%
clearControls() %>%
addLegend(
"bottomleft",
pal = pal,
na.label = "Data unavailable",
values = healthMapData()@data$urban_rural_type,
labFormat = function(type, cuts, p) {
cuts[1] = "Large - in a metro area with at least 1 million residents or more"
cuts[2] = "Small - in a metro area with fewer than 1 million residents"
cuts[3] = "Micropolitan adjacent to a large metro area"
cuts[4] = 'Noncore adjacent to a large metro area'
cuts[5] = 'Micropolitan adjacent to a small metro area'
cuts[6] = 'Noncore adjacent to a small metro and contains a town of at least 2,500 residents'
cuts[7] = 'Noncore adjacent to small metro area and does not contain a town of at least 2,500 residents'
cuts[8] = 'Micropolitan area not adjacent to a metro area'
cuts[9] = 'Noncore adjacent to micro area and contains a town of at least 2,500 residents'
cuts[10] = 'Noncore adjacent to micro area and does not contain a town of at least 2,500 residents'
cuts[11] = 'Noncore not adjacent to metro or micro area and contains a town of at least 2,500 residents'
cuts[12] = 'Noncore not adjacent to metro or micro area and does not contain a town of at least 2,500 residents'
paste0(cuts)},
title = "Level of urban- or rural-ness",
layerId = "countyLegend")
这段代码的labFormat
展示了图例的样子,其中viridis
的紫色是图例中最上面的颜色,对应于{{1 }},并被分配了“大型-至少有100万居民的都市圈”标签。
我也看到了有关如何反转图例的示例代码:
urban_rural_type
但是我正在努力查看如何将两个功能合并到一个 labFormat = labelFormat(transform = function(x) sort(x, decreasing = TRUE)
中。
理想情况下,我的最终结果将是一个传说,该传说源于黄色->紫色(顶部->底部),标签上贴有“非核心区域,不与市区或微小区域相邻,并且不包含至少有2500名居民的城镇” ->“大-至少有一百万居民或以上的都会区。”