2015-02-24 64 views
1

我努力學習與rCharts的單張功能,並想繪製多個標記和彈出窗口從data.frame對象囊括在R使用單張及rCharts從緯度和經度數據幀繪製標記值

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's rCharts page上的示例代碼。這是我的修改:

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) 

此代碼不會產生任何標記。我正在尋找一種解決方案,我可以爲每次觀察繪製經緯度和緯度,並在位置欄中填入由彈出式填充的彈出式標記。

回答

3

這是行不通的,因爲DF $緯度返回一個向量在您的數據框中的所有緯度:

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,現在無法測試。

+0

只是爲了澄清,如果另一個新手這個,我在我原來的帖子中加入@ iH8的代碼來代替'map $ marker'這一行。 – user3334472 2015-02-24 19:29:42