2016-05-15 36 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

太棒了!非常感謝你們!幾乎完成了這個! –

相關問題