使用colorQuantile时,AddLegend显示值范围而不是百分比 [英] AddLegend Display Value range instead of percentage when using colorQuantile
问题描述
我正在使用R闪亮仪表板显示天气栅格文件.我使用colorQuantile作为显示颜色的方法.在图例中,它显示了百分比.如您在下图中看到的.我希望图例中的标签显示每个bin的值范围.我不确定该怎么做.
I am using R shiny-dashboard to display a weather raster file. I used colorQuantile as the method to display the color. In the legend, it shows the percentage. As you can see in the following image. I want the labels in the legend show the range of the value instead for each bin. I am not sure how to do that.
这是我的服务器.R
output$weather_map <- renderLeaflet({
rw = weatherband()
if (!is.null(rw)) {
pal_w = colorQuantile('RdYlGn', values(rw), na.color = 'transparent', n = 7)
leaflet() %>%
addTiles() %>%
addRasterImage(rw, colors = pal_w, opacity = 0.5) %>%
addLegend(position = 'topright', pal = pal_w, value = raster::values(rw), opacity = 1)
}
})
注意:rw是光栅图像.
Note: rw is the raster image.
先谢谢您!
推荐答案
当我需要调整 leaflet
中的标签时,我会转而使用参数 colors
和 labels
,而不是 pal
和 values
.好处是您可以自定义缺点是多行代码.
When I need want to adjust the labels in leaflet
I fall back to using the arguments colors
and labels
instead of pal
and values
. The upside is you can customize the downside is a few more lines of code.
由于我无权访问 rw
,因此我抓到了我最喜欢的地图示例:
Since I don't have access to rw
I'm grabbing my favorite map example:
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
然后使用 leaflet
备用模式来自定义图例标签:
Then the leaflet
alternative pattern for customizing the legend labels:
library(leaflet)
qpal <- colorQuantile("RdYlBu", nc$AREA, n = 5)
# the extra code building off the existing pal
qpal_colors <- unique(qpal(sort(nc$AREA))) # hex codes
qpal_labs <- quantile(nc$AREA, seq(0, 1, .2)) # depends on n from pal
qpal_labs <- paste(lag(qpal_labs), qpal_labs, sep = " - ")[-1] # first lag is NA
map %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1,
color = ~qpal(AREA)
) %>%
addLegend(colors = qpal_colors, labels = qpal_labs, opacity = 1)
这篇关于使用colorQuantile时,AddLegend显示值范围而不是百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!