在下面的示例中,我的城市位于传单Map上。我希望弗吉尼亚州的城市的填充颜色为黄色,北卡罗来纳州的城市的填充颜色为绿色。如何在leaflet
Map上按组设置圆圈标记的填充颜色?
例如
# Import needed libraries
library(tidyverse)
library(leaflet)
library(sf)
# Create example dataset
aa <- data.frame(
state = c('Virginia','Virginia','North Carolina', 'North Carolina'),
city = c('Richmond','Charlottesville', 'Raleigh', 'Charlotte'),
lat = c(37.53,38.01,35.78,35.22),
lon = c(-77.44,-78.47,-78.63,-80.84)) %>%
st_as_sf(coords = c('lon', 'lat'), crs = 4269) %>%
mutate(lat = st_coordinates(.)[, 2],
lon = st_coordinates(.)[, 1])
# Make map (this fills all points red)
aa %>%
leaflet(options = leafletOptions(attributionControl = F,
zoomControl = F)) %>%
addTiles() %>%
addProviderTiles("Esri.WorldImagery") %>%
setView(-78.47,
36.53,
zoom = 7) %>%
addCircleMarkers(lng = ~lon,
lat = ~lat,
fillColor = 'red',
fillOpacity = 1,
color = 'black',
stroke = TRUE,
weight = 2,
radius = 5)
字符串
的数据
- 下面是一个similar SO question,用于根据连续变量更改填充颜色
1条答案
按热度按时间0sgqnhkj1#
对于离散变量,您可以使用
leaflet::colorFactor
创建一个调色板。然后可以使用该调色板根据state
列分配fillColor
:字符串
的数据