2016-05-15 51 views
3

我正在试图绘制位于墨尔本的所有树木的地图。 到我使用的数据集的链接是在这里 - Melbourne Urban Tree Data根据一个因素分配不同的图标

在数据集我想基于列名“属”,这看起来是这样分配不同的图标: enter image description here

眼下我能够得到各界的最后情节: enter image description here

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

library(leaflet) 
library(dplyr) 

td <- read.csv("treedata.csv", header = TRUE) 
m <- leaflet(td) %>% addTiles('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>') 
m %>% addCircles(~Longitude, ~Latitude, popup=paste("Name:", td$CommonName), weight = 3, radius=3, 
       color="#ffa500", stroke = TRUE, fillOpacity = 0.8) 
+3

您可以使用自己的标记,你只需要该'.png's。请参阅[这里](https://rstudio.github.io/leaflet/markers.html)。有一个问题,你是如何让小册子有很多点的? – TimSalabim

+0

您可以使用'addCircles()'函数并查看本教程 - [http://trendct.org/2015/06/26/tutorial-how-to-put-dots-on-a-leaflet-map -with-R /]。希望这可以帮助。 –

+0

TimSalabim,我曾经在此工作过一段时间,并能够绘制一大堆点https://github.com/rstudio/leaflet/pull/174#issuecomment-135988304 – timelyportfolio

回答

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

+3

小错字:改变'“oher”在其他变异中(Genus = factor(ifelse(Genus%in%c(“Quercus”,“Corymbia”,“Platanus”,“Ulmus”,“Eucalyptus”),Genus,“other”)) )' –

+0

oops,thx @ G.Cocca :) – lukeA

+0

太棒了!非常感谢你们!几乎完成了这个! –