根据因子分配不同的图标

时间:2016-05-15 07:15:54

标签: r leaflet

我正在尝试绘制位于墨尔本的所有树木的地图。 我正在使用的数据集的链接就在这里 - Melbourne Urban Tree Data

在数据集中,我想根据列名“Genus”分配不同的图标,如下所示: enter image description here

现在我可以在最后的情节中获得圆圈: enter image description here

到目前为止我使用的代码:

General

1 个答案:

答案 0 :(得分:4)

正如@TimSalabim的评论中提到的,尝试使用带图标的标记。为了它的乐趣:

library(leaflet)
library(dplyr)
library(readr)
download.file("https://data.melbourne.vic.gov.au/api/views/fp38-wiyy/rows.csv?accessType=DOWNLOAD", tf <- tempfile(fileext = ".csv"))
set.seed(1)
td <- read_csv(tf) %>% 
  sample_n(500) %>% 
  mutate(Genus = factor(ifelse(Genus %in% c("Quercus", "Corymbia", "Platanus", "Ulmus", "Eucalyptus"), Genus, "other"))) 
m <- leaflet(td) %>% 
  addTiles(urlTemplate = 'http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
           attribution='Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>')
myicons <- iconList(
  Quercus = makeIcon("http://i.stack.imgur.com/okgqd.png",iconWidth = 8, iconHeight = 8),
  Corymbia = makeIcon("http://i.stack.imgur.com/nfGZT.png",iconWidth = 8, iconHeight = 8),
  Platanus = makeIcon("http://i.stack.imgur.com/J47uj.png",iconWidth = 8, iconHeight = 8),
  Ulmus = makeIcon("http://i.stack.imgur.com/idnpO.png",iconWidth = 8, iconHeight = 8),
  Eucalyptus = makeIcon("http://i.stack.imgur.com/6GzzW.png",iconWidth = 8, iconHeight = 8),
  other = makeIcon("http://i.stack.imgur.com/x0bOg.png",iconWidth = 8, iconHeight = 8)
)
m %>% addMarkers(~Longitude, ~Latitude, popup=paste("Name:", td$`Common Name`),
                 icon = ~myicons[Genus])

给出类似的东西:

enter image description here

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here