【发布时间】:2019-11-29 20:40:46
【问题描述】:
这个问题的所有文件都在following git:
我正在尝试在带有分类的传单中绘制分类地图 将字符向量显示为标签的图例:
加载包
首先我加载所需的包
library(rgdal)
library(raster)
library(kableExtra)
library(rasterVis)
library(leaflet)
然后我加载所需的数据集
Codes <- readRDS("Codes.rds")
LandCover <- readRDS("LandCover.rds")
如果我们查看土地覆盖栅格,它是一个分类栅格,我
用光栅包中的ratify 制作
LandCover
## class : RasterLayer
## dimensions : 832, 680, 565760 (nrow, ncol, ncell)
## resolution : 30.00002, 29.99993 (x, y)
## extent : 288800.8, 309200.8, 6367272, 6392231 (xmin, xmax, ymin, ymax)
## crs : +proj=utm +zone=19 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
## source : memory
## names : LC_CHILE_2014_b
## values : 150, 932 (min, max)
## attributes :
## ID names IDs
## from: 150 Cultivos 150
## to : 932 Suelos rocosos 932
我希望将 name 属性作为图例
使用 rasterVis 包中的 levelplot 函数工作:
rasterVis::levelplot(LandCover)
如果有效,数据也会在 Codes data.frame 中重复
kable(Codes, caption = "Tabla de atributos del mapa") %>%
kable_styling(bootstrap_options = c("striped", "hover"))
地图属性表
代码
已选中
150
栽培品种
212
Nativo de Hoja Ancha
251
Plantaciones de bosque introducido
330
糊状
450
马托拉莱斯
510
胡梅代尔
640
库尔波德阿瓜
800
上层防水材料
920
沙棘
932
洛科索斯
两个测试无效
测试 1
pal <- colorFactor(rainbow(10), values(LandCover),
na.color = "transparent")
leaflet() %>% addTiles() %>% addRasterImage(LandCover, colors = pal, opacity = 0.8) %>% addLegend(pal = pal, values = values(LandCover),title = "Land Cover", labels = Codes$Selected)
测试 2
在第二个中,我认为将标签属性添加到
addLegend 函数可以解决问题,但它不起作用
pal <- colorFactor(rainbow(10), values(LandCover),
na.color = "transparent")
leaflet() %>% addTiles() %>% addRasterImage(LandCover, colors = pal, opacity = 0.8) %>% addLegend(pal = pal, values = values(LandCover),title = "Land Cover", labels = Codes$Selected)
【问题讨论】: