[看我的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。
您的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
)
如果您有一个数字矢量想要将图标分配到不同的范围,您也可以使用cut
或findInterval
。>