user3334472
user3334472

Reputation: 149

Scaled circles with rCharts and leaflet based on a point feature

I made a map with markers using rCharts and Leaflet with the code below.

library(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), capacity = c(30, 48, 2, 80, 5, 18))
df
          location latitude longitude capacity
1      White House 38.89710 -77.03655       30
2      Impound Lot 38.81289 -77.01720       48
3      Bush Garden 38.94178 -77.07331        2
4          Rayburn 38.88678 -77.01053       80
5  Robertson House 38.90539 -77.06164        5
6 Beers Elementary 38.86466 -76.95554       18

map <- Leaflet$new()
map$setView(c(38.89710, -77.03655), 12)
map$tileLayer(provider = 'Stamen.TonerLite')
for (i in 1:nrow(df)) {
  map$marker(c(df[i, "latitude"], df[i, "longitude"]), bindPopup = df[i, "location"])
}
print(map)

I am now interesting in having a circle scaled to df$capacity instead of a marker. I attempted to substitute map$marker with map$circle but had no luck.

Something along the lines of this, is what I am trying to accomplish.

Upvotes: 0

Views: 170

Answers (1)

cory
cory

Reputation: 6659

Couldn't get it going with rMaps (which has replaced rCharts for maps), but I got the 'leaflet' package working...

devtools::install_github("rstudio/leaflet")
library(leaflet)
leaflet() %>% 
  addTiles(urlTemplate="http://{s}.tile.stamen.com/toner-lite/{z}/{x}/{y}.png") %>% 
  setView(-77.03655, 38.89710, zoom = 12) %>% 
  addCircles(data = df, lat = ~ latitude, lng = ~ longitude, radius = ~ capacity)

Upvotes: 1

Related Questions