简体   繁体   English

leaflet 图例和调色板的对数刻度

[英]Logarithmic scale for leaflet legend and color palette

I am plotting GIS data using leaflet in R and I am setting the colour of the plotted points to a value in the data set.我在 R 中使用 leaflet 绘制 GIS 数据,并将绘制点的颜色设置为数据集中的一个值。 The data values span a wide range and it is important to distinguish colour differences at the lower end of the values but also important to differentiate the higher values.数据值范围很广,区分值低端的颜色差异很重要,但区分较高值也很重要。 To get the effect I'm looking for, I need to be able to use a logarithmic scale to determine the colours of the points and the legend scale.为了获得我正在寻找的效果,我需要能够使用对数刻度来确定点的颜色和图例刻度。

The code below gives an example of the data and the way in which I'm plotting it now but I don't know how to use a logarithmic scale for the colours and the legend.下面的代码给出了一个数据示例以及我现在绘制它的方式,但我不知道如何对颜色和图例使用对数刻度。

# Some fake data
df <- sp::SpatialPointsDataFrame(
  cbind(
    (runif(100) - .5) * 10 - 90.620130,  # lng
    (runif(100) - .5) * 3.8 + 25.638077  # lat
  ),
  data.frame(testData = rexp(100, rate = 5) * 1000)
)

# Fomulate a colour palette
pal <- colorNumeric(
  palette = "YlGnBu",
  domain = df$testData
)

leaflet(df) %>% addTiles() %>%
  # Add circle markers coloured by the testData
  addCircleMarkers(fillColor = ~pal(testData), fillOpacity = 0.7, radius=6, stroke=FALSE) %>%
  # Add the legend
  addLegend("bottomright", pal = pal, values = ~testData,
    title = "Test Result",
    opacity = 1
)

Can anyone suggest how I could do this?谁能建议我如何做到这一点?

You could do this:你可以这样做:

 pal <- colorNumeric(palette = "YlGnBu",domain = log10(df$testData))

 leaflet(df) %>%
 addTiles() %>% 
addCircleMarkers(
fillColor = ~pal(log10(testData)), 
fillOpacity = 0.7, 
radius=6, 
stroke=FALSE) %>% 
addLegend("bottomright", 
pal = pal, 
values = ~log10(testData),
 title = "Test Result",
 opacity = 1)

So, you could put log10() in 3 places above to get log scale color differentiation, however this leaves legend also in log scale which is not really friendly for most people?因此,您可以将 log10() 放在上面的 3 个位置以获得对数刻度颜色区分,但是这也会在对数刻度中留下传说,这对大多数人来说并不友好? Did you find an answer, or anybody else?你找到答案了吗,还是其他人?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM