Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Leaflet is a great way to display spatial information in an interactive way. If you want to display the difference between different neighborhoods you would usually get the proper shapefiles on the web and connect your data to them. But sometimes it does not need detailed shapefiles and you want more abstraction to get your information across. I came up with the idea to draw my own little simplified polygons to get an abstract map of Hamburg.
There are some great and free tools on the web to create your own polygons. I was using click2shp. You are just going to draw your polygons on a google map and afterwards you can export your polygons as a shapefile to use them from within R. Down below you find a little R script to display your polygons in a Shiny App.
############################################################################################################################################# # PACKAGES ############################################################################################################################################# require(leaflet) require(shinythemes) require(rgdal) require(maptools) require(rmapshaper) require(shiny) require(leaflet.extras) ############################################################################################################################################# # UI ############################################################################################################################################# shinyUI( bootstrapPage(theme = shinytheme("united"), navbarPage(title="Where to live in Hamburg?", tabPanel("Karte", div(class="outer", tags$style(type = "text/css", ".outer {position: fixed; top: 50px; left: 0; right: 0; bottom: 0; overflow: hidden; padding: 0}"), leafletOutput("mymap", width = "100%", height = "100%") ))))) ############################################################################################################################################# # SERVER ############################################################################################################################################# shinyServer( function(input, output, session) { # setwd setwd("YourPath") # load your own shapes hhshape <- readOGR(dsn = ".", layer = "click2shp_out_poly") # load some data (could be anything) data <- read.csv("anwohner.csv", sep = ";", header = T) rownames(data) <- data$ID hhshape <- SpatialPolygonsDataFrame(hhshape, data) # remove rivers from sp file hhshape <- hhshape[!(hhshape$Stadtteil %in% c("Alster","Elbe","Nix")), ] # create a continuous palette function pal <- colorNumeric( palette = "Blues", domain = hhshape@data$Anwohner ) # plot map output$mymap <- renderLeaflet({ leaflet(options = leafletOptions(zoomControl = FALSE, minZoom = 11, maxZoom = 11, dragging = FALSE)) %>% setView(lng = 9.992924, lat = 53.55100, zoom = 11) %>% addPolygons(data = hhshape, fillColor = ~pal(hhshape@data$Anwohner), fillOpacity = 1, stroke = T, color = "white", opacity = 1, weight = 1.2, layerId = hhshape@data$ID, highlightOptions = highlightOptions(color= "grey", opacity = 1, fillColor = "grey", stroke = T, weight = 12, bringToFront = T, sendToBack = TRUE), label=~stringr::str_c(Stadtteil,' ',"Anwohner:",formatC(Sicherheit, big.mark = ',', format='d')), labelOptions= labelOptions(direction = 'auto')) }) })
This little R Code will give you the following result.
Make sure you check out my Github for other data driven projects.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.