更新为“R传单连续图例中的逆序”?

问题描述 投票:0回答:1

我的问题与 5 年前的问题完全相同。我想反转从 R 创建的传单等值线中连续色标的比例。我希望现在有比那时更好的答案。

R 传单连续图例中的逆序

这是我的代码: (dput()ed 数据可在此处获取:https://drive.google.com/file/d/1asaNqvtm0VXGH-o7GoWVd3Q6FSm1RNES/view


pal <- colorNumeric("magma", NULL)

map.positivity.leaflet <-
    leaflet(data=D) %>%
    setView(lng=-72.8, lat=41.5, zoom=9) %>%
    addPolygons(color="lightgrey",      # stroke color
                stroke = TRUE,
                weight = 1.5,           # stroke
                fillColor = ~pal(town.positivity),
                fillOpacity = 1,
                label = ~text,
                labelOptions = labelOptions(textsize = "15px", ## could also set "font-size" in 'style' arg
                                            style = list( # add custom CSS
                                                "background-color" = "darkgreen",
                                                "color" = "white" # font color
                                            ))) %>%
    addLegend(
        "bottomright",
        pal = pal,
        values = ~town.positivity,
        title = "<b>Test Positivity (%)</b>",
        opacity = 1
    )


这是我得到的:

我希望色阶从底部的 0 到顶部的最大值。有没有比上面引用的 5 年前的问题的答案中更简单的方法来做到这一点?

r leaflet legend choropleth
1个回答
0
投票

嘿, 您需要两个调色板(pal 和 pla_2)。 pal_2 与 legende 相反
您需要此代码来反转 addLedgend 中的顺序: labFormat = labelFormat(transform = function(x) sort(x,ducing = TRUE)

pal <- colorNumeric("magma", NULL)
pal_2< colorNumeric("magma", NULL, reverse = TRUE)
map.positivity.leaflet <-
   leaflet(data=D) %>%
   setView(lng=-72.8, lat=41.5, zoom=9) %>%
   addPolygons(color="lightgrey",      # stroke color
          stroke = TRUE,
          weight = 1.5,           # stroke
          fillColor = ~pal(town.positivity),
          fillOpacity = 1,
          label = ~text,
          labelOptions = labelOptions(textsize = "15px", ## could also set "font-size" in 'style' arg
                                      style = list( # add custom CSS
                                        "background-color" = "darkgreen",
                                        "color" = "white" # font color
                                      ))) %>%
 addLegend(
  "bottomright",
  pal = pal_2,
  values = ~town.positivity,
  title = "<b>Test Positivity (%)</b>",
  labFormat = labelFormat(transform = function(x)  sort(x, decreasing = TRUE)
  opacity = 1
  )
© www.soinside.com 2019 - 2024. All rights reserved.