0

I got time related data (7 years) displayed in a choropleth map, using R and Leaflet-for-R-package. Therefore use a kmz-file for geometry and csv data for attributes. So far I tried it with data for 2 years, using this tutorial:

http://journocode.com/2016/01/28/your-first-choropleth-map/

Following the example, I used my data instead of the given one in the tutorial. Everything worked fine. But longterm, I want to add all my 7 years data, therefore it would be useful to replace the radio button-based layer control by a (time)slider - changing the overlays according to the year.

In a comparable case here on stackoverflow, someone worked with geojson-files as geometry in order to use the timeslider plugin for Leaflet: Leaflet slider group by year

So do I need to change my geom data into geojson as well? And do so, how I link my csv-data to geojson and will R be able to cover all? I hope you might have some suggestions...

https://github.com/Pippo87/R-leaflet-choropleth

Here is my R-script:

library(rgdal)
berlin <- readOGR("LOR-Planungsraeume.kml","LOR_Planungsraum", encoding="utf-8")
plot(berlin)


Auslaender2007 <- read.csv("LOR_Auslaender_2007.csv", encoding="latin1", sep=",", dec=".")
Auslaender2008 <- read.csv("LOR_Auslaender_2008.csv", encoding="latin1", sep=",", dec=".")

library(leaflet)

palette <- colorBin(c('#fef0d9',
                  '#fdd49e',
                  '#fdbb84',
                  '#fc8d59',
                  '#e34a33',
                  '#b30000'),
                Auslaender2008$ANTEIL, bins = 6, pretty=TRUE, alpha = TRUE)

popup2007 <- paste0("<strong>Auslaender 2007</strong></span>",
             "<br><strong>LOR </strong></span>", 
             Auslaender2007$LORNAME, 
             "<br><strong> Relativer Auslaenderanteil </strong></span>", 
             Auslaender2007$ANTEIL
             ,"<br><strong>Absoluter Auslaenderanteil</strong></span>", 
             Auslaender2007$AUSLAENDER)

popup2008 <- paste0("<strong>Auslaender 2007</strong></span>",
             "<br><strong>LOR </strong></span>", 
             Auslaender2008$LORNAME, 
             "<br><strong> Relativer Auslaenderanteil </strong></span>", 
             Auslaender2008$ANTEIL
             ,"<br><strong>Absoluter Auslaenderanteil</strong></span>", 
             Auslaender2008$AUSLAENDER)

mymap <- leaflet() %>% 
addProviderTiles("Esri.WorldGrayCanvas", options = tileOptions(minZoom=10, maxZoom=16)) %>% 

addPolygons(data = berlin, 
          fillColor = ~palette(Auslaender2007$ANTEIL),  
          fillOpacity = 1,         
          color = "darkgrey",       
          weight = 1.5,           
          group="<span style='font-size: 11pt'><strong>2007</strong></span>")%>%  

addPolygons(data = berlin, 
          fillColor = ~palette(Auslaender2008$ANTEIL), 
          fillOpacity = 1, 
          color = "darkgrey", 
          weight = 1.5, 
          popup = popup2008, 
          group="<span style='font-size: 11pt'><strong>2008</strong></span>")%>%

addLayersControl(
baseGroups = c("<span style='font-size: 11pt'><strong>2007</strong></span>", "<span style='font-size: 11pt'><strong>2008</strong></span>"),
options = layersControlOptions(collapsed = FALSE))%>% 

addLegend(position = 'topleft', pal = palette, values = Auslaender2008$ANTEIL, opacity = 1, title = "Relativer<br>Auslaenderanteil") 


print(mymap)
Community
  • 1
  • 1
  • Have a look at [this](http://stackoverflow.com/questions/36554605/cant-loop-with-rs-leaflet-package-to-produce-multiple-maps/36587525#36587525) answer from @timelyportfolio. I think this is what you want. – TimSalabim Jun 08 '16 at 13:49
  • Thanks for your response! But you created your own dataframe and convert it to a geojson file. Like I said, my dataframe consists from a kmz data (geometry) and 2 (or more..) correlating csv tables. So, how to implement the slider then? For the kmz it wouldnt be any problem, could use ogr2ogr web client or qgis pulgin to deal with that. Still dont know how to deal with csv and json to correspond... Sorry, all new to that. – Philippo Storino Jun 08 '16 at 15:30
  • 1
    `sp::merge` would be the way to merge spatial data with non-spatial dataframes. I am assuming that `berlin <- readOGR("LOR-Planungsraeume.kml","LOR_Planungsraum", encoding="utf-8")` gives you a Spatial*(DataFrame) object and your `Auslaender2007` (and 2008) have some column with shared IDs to the SP*DF – TimSalabim Jun 08 '16 at 16:16
  • Exactly! I followed the tutorial, mentioned in the link above and the merge worked without any further function required. Because `addPolygons(data = berlin, fillColor = ~palette(Auslaender2007$ANTEIL)...` is the order to fill the polygons with color according to the csv.data. That worked already fine. I just dont know how to add a timeslider instead of radio buttons in the layer control.. – Philippo Storino Jun 08 '16 at 16:53
  • You basically wanna start where it reads `# add leaflet-timeline as a dependency # to get the js and css` and replace `leaf` with `mymap`. Maybe I'm missing something fundamental here, but it is hard to tell with no reproducible example. – TimSalabim Jun 08 '16 at 16:57
  • I think so... But as I am not sure, I created a github repository with all the files included. I hope that helps you out... https://github.com/Pippo87/R-leaflet-choropleth/tree/master – Philippo Storino Jun 09 '16 at 11:20
  • I've sent you a pull request. However, the actual time slider implementation is beyond me. @timelyportfolio may be able to help here. – TimSalabim Jun 10 '16 at 15:36
  • Did you ever figure this out? – GISHuman Oct 08 '16 at 12:53

0 Answers0