想向传单地图添加10种不同类型的图标

问题描述 投票:0回答:1

[看我的R代码,这里我可以标记两种不同类型的标记图标,但是我想用10种不同类型的标记图标标记quakes $ mag列的所有10个值。

 quakes1 <- quakes[1:10,]

leafIcons <- icons(
  iconUrl = ifelse(quakes1$mag < 4.6,
                   "http://leafletjs.com/examples/custom-icons/leaf-green.png",
                   "http://leafletjs.com/examples/custom-icons/leaf-red.png"
  ),
  iconWidth = 38, iconHeight = 95,
  iconAnchorX = 22, iconAnchorY = 94,
  shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
  shadowWidth = 50, shadowHeight = 64,
  shadowAnchorX = 4, shadowAnchorY = 62
)

leaflet(data = quakes1) %>% addTiles() %>%
  addMarkers(~long, ~lat, icon = leafIcons)

我尝试使用switch语句而不是ifelse来执行此操作,但是它不适用于switch。

r dictionary leaflet icons markers
1个回答
0
投票

您的ifelse是矢量化函数,为leafIcons创建了图标矢量。您可以通过多种方法对switch进行矢量化处理,也可以使用多种方法基于矢量创建leafIcons-请参见this related question。使用dplyr,您可以使用case_when,这可能是您想要的:

library(dplyr)
library(leaflet)

leafIcons <- icons(
  iconUrl = case_when(
    quakes1$mag <= 4.3 ~ "http://leafletjs.com/examples/custom-icons/leaf-green.png",
    4.3 < quakes1$mag & quakes1$mag <= 5 ~ "http://leafletjs.com/examples/custom-icons/leaf-orange.png",
    quakes1$mag > 5 ~ "http://leafletjs.com/examples/custom-icons/leaf-red.png"
  ),
  iconWidth = 38, iconHeight = 95,
  iconAnchorX = 22, iconAnchorY = 94,
  shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
  shadowWidth = 50, shadowHeight = 64,
  shadowAnchorX = 4, shadowAnchorY = 62
)

如果您有一个数字矢量想要将图标分配到不同的范围,您也可以使用cutfindInterval。>

© www.soinside.com 2019 - 2024. All rights reserved.