leaflet - Using R and leafleft for a map that changes over time -
i have data base organized neighborhoods city. data contains economic information month month several years. database looks (for reproducible example, 3 years)
> head(data , 4) neigh_id_shape m1_y1 m2_y1 .... m11_y3 m12_y3 id 1 7 6 -1 -3 id 2 3 -1 -5 3 id 3 -1 6 0 17 id 4 0 3 1 2 more this, have shapefile object of city , neighboords. "neigh_id_shape" variable in data match variable. shapefile, can plot customized map using leafleft
> n_shape <- readshapespatial("path\\my_shape.shp") leaflet() %>% addtiles() %>% setview(lng = center_city_long , lat= center_city_lat ,zoom=11) %>% addpolygons( data= n_shape , weight= 1 ) so, dynamic map (with play button) visualises change on time (along months variables). furthermore, construct 2 scales of colors values of months: red scale , blue scale negative , positive values, respectively.
new
according 1 comemnt, tryed this... not works (see picture after script)
library(htmlwidgets) library(htmltools) library(leaflet) #install.packages("geojsonio") library(geojsonio) llat<- c(-22.973897, -22.972892,-22.971588, 22.972270,-22.974700,-22.969869,-22.970847,-22.968832,-22.974660,-22.975243) llong<- c( -43.185296,-43.187338,-43.185675,-43.185869,-43.190858,-43.186791, -43.188980,-43.187092,-43.193239,-43.193486) #build data.frame 10 obs + 3 cols power <- data.frame( "latitude" = llat,#c(33.515556, 38.060556, 47.903056, 49.71, 49.041667, 31.934167, 54.140586, 54.140586, 48.494444, 48.494444), "longitude" = llong,#c(129.837222, -77.789444, 7.563056, 8.415278, 9.175, -82.343889, 13.664422, 13.664422, 17.681944, 17.681944), "start" = do.call( "as.date", list( x = c("1jan1960", "2jan1960", "31mar1960", "30jul1960", "1jan1961", "2jan1961", "31mar1961", "30jul1961" , "1jan1962", "2jan1962" ), format = "%d%b%y" ) ) ) # set start same end # adjust power$end <- power$start + 30 # use geojsonio convert our data.frame # geojson timeline expects power_geo <- geojson_json(power[,c(1,2)],lat="latitude",lon="longitude", pretty = t) # create leaflet map on build leaf <- leaflet() %>% addtiles() # add leaflet-timeline dependency # js , css leaf$dependencies[[length(leaf$dependencies)+1]] <- htmldependency( name = "leaflet-timeline", version = "1.0.0", src = c("href" = "http://skeate.github.io/leaflet.timeline/"), script = "javascripts/leaflet.timeline.js", stylesheet = "stylesheets/leaflet.timeline.css" ) # use new onrender in htmlwidgets run # code once our leaflet map rendered # did not spend time perfecting leaflet-timeline # options leaf %>% setview(lng = -43.187574 , lat= -22.971875,zoom=14) %>% onrender(sprintf( ' function(el,x){ var power_data = %s; var timelinecontrol = l.timelineslidercontrol({ formatoutput: function(date) { return new date(date).tostring(); } }); var timeline = l.timeline(power_data, { pointtolayer: function(data, latlng){ var hue_min = 120; var hue_max = 0; var hue = hue_min; return l.circlemarker(latlng, { radius: 10, color: "hsl("+hue+", 100%%, 50%%)", fillcolor: "hsl("+hue+", 100%%, 50%%)" }); }, steps: 1000, duration: 10000, showticks: true }); timelinecontrol.addto(htmlwidgets.find(".leaflet").getmap()); timelinecontrol.addtimelines(timeline); timeline.addto(htmlwidgets.find(".leaflet").getmap()); } ', power_geo ))

Comments
Post a Comment