gpt4 book ai didi

r - 将 colorFactor 调色板添加到 mapview 对象

转载 作者:行者123 更新时间:2023-12-05 03:27:19 24 4
gpt4 key购买 nike

目的是为因子值提供固定颜色。我很难将 colorFactor 比例应用于 mapview 对象。但是,调色板似乎与 zcol 不匹配。

我尝试了以下,类似于传单 map 。

library(mapview)

colors <- colorFactor(palette = c("Red", "Green", "Blue"),
levels = c("Oberfranken","Mittelfranken", "Unterfranken"))

mapview(franconia, zcol = "district",col.regions=colors)

Mapview Output

我收到以下错误消息:

1: In col.regions(nregions) :   Some values were outside the color
scale and will be treated as NA

有什么帮助吗?

以下内容适用于传单,但不使用 mapview。

franconia %>% leaflet() %>% addTiles() %>% addPolygons(fillColor = ~colors(district))

leaflet output

最佳答案

我不知道如何使用贝尼的回答来指定我想将哪种颜色应用于 map 上的特定要素。

原来 colorFactor 返回一个函数。您需要提供要用于为要素着色的字段:

library('sf')
library('leaflet')
library('mapview')
# Read in shapefile and keep the first three features
ncShp <- st_read(system.file("shape/nc.shp", package="sf"))[1:3, ]
ncShp
# Simple feature collection with 3 features and 14 fields
# Geometry type: MULTIPOLYGON
# Dimension: XY
# Bounding box: xmin: -81.74107 ymin: 36.23388 xmax: -80.43531 ymax: 36.58965
# Geodetic CRS: NAD27
# AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 geometry
# 1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1 10 1364 0 19 MULTIPOLYGON (((-81.47276 3...
# 2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0 10 542 3 12 MULTIPOLYGON (((-81.23989 3...
# 3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5 208 3616 6 260 MULTIPOLYGON (((-80.45634 3...

cols <- c('red', 'green', 'blue')

# Colour our features sequentially based on the NAME field
ncShp$NAME
# [1] "Ashe" "Alleghany" "Surry"
# Ashe will be red, Alleghany will be green, Surry will be blue

colPal <- leaflet::colorFactor(palette = cols, levels = ncShp$NAME)
# Note: use levels(ncShp$NAME) if it's already a factor

# Send it to mapview
mapview::mapview(ncShp, zcol='NAME', col.regions=colPal(ncShp$NAME))

enter image description here

关于r - 将 colorFactor 调色板添加到 mapview 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71526164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com