簡體   English   中英

使用帶有lat和lon值的數據框中的小葉和rCharts繪制標記

[英]Plotting markers using leaflet and rCharts from a data frame with lat and lon values

我正在嘗試使用rCharts學習Leaflet功能,並希望繪制從Rdata.frame對象獲取的多個標記和彈出窗口

df <- data.frame(location = c("White House", "Impound Lot", "Bush Garden", "Rayburn", "Robertson House", "Beers Elementary"), latitude = c(38.89710, 38.81289, 38.94178, 38.8867787, 38.9053894, 38.86466), longitude = c(-77.036545, -77.0171983, -77.073311, -77.0105317, -77.0616441, -76.95554))
df
          location latitude longitude
1      White House 38.89710 -77.03655
2      Impound Lot 38.81289 -77.01720
3      Bush Garden 38.94178 -77.07331
4          Rayburn 38.88678 -77.01053
5  Robertson House 38.90539 -77.06164
6 Beers Elementary 38.86466 -76.95554

我嘗試在Ramnanth的rCharts頁面上修改示例中的代碼。 這是我的修改:

map <- Leaflet$new()
map$setView(c(38.89710, -77.03655), 12)
map$tileLayer(provider = 'Stamen.TonerLite')
map$marker(c(df$latitude, df$longitude), bindPopup = df$location)

此代碼不會生成任何標記。 我正在尋找一種解決方案,我可以為每次觀察繪制緯度和經度,並且有一個帶有彈出窗口的標記,該彈出窗口由位置列中的值填充。

這不起作用,因為df $ latitude返回一個包含數據幀所有緯度的向量:

df$latitude
## [1] 38.89710 38.81289 38.94178 38.88678 38.90539 38.86466
df$longitude
##b[1] -77.03655 -77.01720 -77.07331 -77.01053 -77.06164 -76.95554

您需要在循環中添加標記:

for (i in 1:nrow(df)) {
    map$marker(c(df[i, "latitude"], df[i, "longitude"]), bindPopup = df[i, "location"])
}

注意:這是快速的,徒手的,剛開始使用R,我現在無法測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM