0

I have a data base organized by neighborhoods for some city. The data contains economic information month by month for several years. My database looks like this (for a 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 than this, I have a shapefile object of the city and their neighboords. The "neigh_id_shape" variable in the data is the match variable. With this shapefile, I can plot a 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, I would like to do a dynamic map (with a play button) which visualises the change over time (along the months variables). Furthermore, I would like construct two scales of colors for the values of the months: red scale and blue scale for negative and positive values, respectively.

New

According with one comemnt, I tryed this... but It does not works (see the picture after the 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 with 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 as end
#  adjust however you would like
power$end <- power$start + 30


# use geojsonio to convert our data.frame
#  to GeoJSON which timeline expects
power_geo <- geojson_json(power[,c(1,2)],lat="Latitude",lon="Longitude", pretty = T)

# create a leaflet map on which we will build
leaf <- leaflet() %>%
  addTiles()

# add leaflet-timeline as a dependency
#  to get the js and 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 the new onRender in htmlwidgets to run
#  this code once our leaflet map is rendered
#  I did not spend time perfecting the 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
    ))

enter image description here

MAOC
  • 625
  • 2
  • 8
  • 26
  • 1
    For the timeline, unfortunately that feature is not yet integrated into R's leaflet (it is in the pipeline though). But last year, a workaround was developed - [link](https://stackoverflow.com/questions/36554605/cant-loop-with-rs-leaflet-package-to-produce-multiple-maps/36587525#36587525). Please take a look at that and see if you can make it work for your case. – Jul Sep 12 '17 at 15:01
  • 1
    I edited my question with my attempt – MAOC Sep 12 '17 at 17:50

0 Answers0