简体   繁体   English

传单:栅格的自定义bin图例

[英]Leaflet: Custom bin legend for raster

I've been playing around with the leaflet package and can't create a proper legend for a raster. 我一直在使用leaflet包,无法为栅格创建适当的图例。

The problem is as follows: I've created an informative map with this code: 问题如下:我使用以下代码创建了内容丰富的地图:

raster file: https://drive.google.com/open?id=0B7mw858dxh5MODZqSHNHbFR3clU 栅格文件: https : //drive.google.com/open?id=0B7mw858dxh5MODZqSHNHbFR3clU

library(raster)
library(leaflet)
r <- raster('raster.tif')
colores <- c('red', 'green', 'blue', 'chocolate', 'deeppink', 'grey')
at <- seq(0, 4800, 800)
cb <- colorBin(palette = colores, bins = length(at), domain = at)

leaflet() %>% 
    setView(-67.5,-16, zoom = 7) %>%
    addRasterImage(r, colors = cb) %>%
    addLegend(pal = cb, values = at)

Variable r is a raster with precipitation data ranging from 171 to 4667 mm/year. 变量r是一个栅格,其降水数据范围为171至4667 mm /年。

Output: 输出:

https://i.imgur.com/bt7Ew6O.png https://i.imgur.com/bt7Ew6O.png

I want to get a legend with 6 bins using the colors passed to the variable colores but the output shows 10 bins ignoring the ranges I passed to the at variable. 我想使用传递给变量colores的颜色获得具有6个bin的图例,但输出显示10个bin,而忽略了我传递给at变量的范围。

How should I achieve what I want? 我应该如何实现自己想要的?

You simply need to replace length(at) with at in your colorBin call 您只需要在colorBin调用中用at替换length(at)

library(raster)
library(leaflet)
r <- raster('raster.tif')
colores <- c('red', 'green', 'blue', 'chocolate', 'deeppink', 'grey')
at <- seq(0, 4800, 800)
cb <- colorBin(palette = colores, bins = at, domain = at)

leaflet() %>% 
    setView(-67.5,-16, zoom = 7) %>%
    addRasterImage(r, colors = cb) %>%
    addLegend(pal = cb, values = at)

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

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