Package Dependencies/ Data

N.B. There are codeblocks here which have been hidden by default as they are just repeats of the start of ‘code’. This just the point where in a new environment you would need to load up the same packages as before, and import/create the data in ‘code’, to make the below graphics produceable in R. Anything new starts after ///.

If you’d like to see this code, it’s visible in the .rmd at ‘Map_Code.Rmd’.. Or directly from github https://github.com/fergustaylor/Arran (in /docs).

///

Mapping

Load the packages

library(rgdal)
library(leaflet)
library(ggmap)

Coordinates

postcodelist <- paste(unique(arrancoordinates$listID), "Postcodes", sep=" ")
datazonelist <- paste(unique(arrancoordinates$listID), "Datazones", sep=" ")

m = leaflet() %>% addTiles() %>% setView(-5.227680, 55.582338, zoom = 10) 

Create some labels for the maps.

Map1

m %>% 

#allcoordinates
addMarkers(
    lng = arrancoordinates$longitude, lat = arrancoordinates$latitude,
    label = arrancoordinates$postcode,
    labelOptions = labelOptions(noHide = F), group = "Postcode Plots") %>%
hideGroup("All Postcode Plots") %>% 

#alldatazones  
addPolygons(data=exampleshapes, 
            weight = 2, 
            label = datazonelist,
            group = "All Datazones") %>% 
hideGroup("Datazones") %>% 
  
#selectcoordinates
addMarkers(
    lng = arrancoordinates$longitude, lat = arrancoordinates$latitude,
    label = arrancoordinates$postcode,
    labelOptions = labelOptions(noHide = F), group = arrancoordinates$listID) %>% 
hideGroup(arrancoordinates$listID) %>% 

#selectdatazone
addPolygons(data = exampleshapes[1] , 
            weight = 2, label = datazonelist[1], group = datazonelist[1]) %>% 
addPolygons(data = exampleshapes[2] , 
            weight = 2, label = datazonelist[2], group = datazonelist[2]) %>% 
addPolygons(data = exampleshapes[3] , 
            weight = 2, label = datazonelist[3], group = datazonelist[3]) %>% 
addPolygons(data = exampleshapes[4] , 
            weight = 2, label = datazonelist[4], group = datazonelist[4]) %>% 
addPolygons(data = exampleshapes[5] , 
            weight = 2, label = datazonelist[5], group = datazonelist[5]) %>% 
addPolygons(data = exampleshapes[6] , 
            weight = 2, label = datazonelist[6], group = datazonelist[6]) %>% 
addPolygons(data = exampleshapes[7] , 
            weight = 2, label = datazonelist[7], group = datazonelist[7]) %>% 
hideGroup(datazonelist[1]) %>%
hideGroup(datazonelist[2]) %>%
hideGroup(datazonelist[3]) %>%
hideGroup(datazonelist[4]) %>%
hideGroup(datazonelist[5]) %>%
hideGroup(datazonelist[6]) %>%
hideGroup(datazonelist[7]) %>%

#Layers control
addLayersControl(
    baseGroups = c("All Datazones", "Postcode Plots", "Nothing"),
    overlayGroups = c(arrancoordinates$listID, datazonelist),
    options = layersControlOptions(collapsed = TRUE)
  )

Example Markers

Inputing example markers.

cliniccoordinates <- read.csv("../alldata/clinics.csv") %>%
dplyr::left_join(arrancoordinates, by="postcode")
#change to character
cliniccoordinates$X <- as.character(cliniccoordinates$X)

Map2

m %>%

#allcoordinates
addMarkers(
    lng = arrancoordinates$longitude, lat = arrancoordinates$latitude,
    label = arrancoordinates$postcode,
    labelOptions = labelOptions(noHide = F), group = "All Postcode Plots") %>%
hideGroup("All Postcode Plots") %>% 

#alldatazones  
addPolygons(data=exampleshapes, 
            weight = 2, 
            label = datazonelist,
            group = "All Datazones",
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("All Datazones") %>% 

#cliniccoordinates
addMarkers(
    lng = cliniccoordinates$longitude, lat = cliniccoordinates$latitude,
    label = cliniccoordinates$X,
    labelOptions = labelOptions(noHide = F), group = "All GP clinics") %>%
  hideGroup("All GP clinics") %>%   

#cliniccoordinates
addMarkers(
    lng = cliniccoordinates$longitude, lat = cliniccoordinates$latitude,
    label = cliniccoordinates$X,
    labelOptions = labelOptions(noHide = F), group = cliniccoordinates$X) %>%
  hideGroup(cliniccoordinates$X) %>%   
  
#Layers control
addLayersControl(
    baseGroups = c("All Datazones", "All Postcode Plots", "All GP clinics", "Nothing"),
    overlayGroups = c(cliniccoordinates$X),
    options = layersControlOptions(collapsed = TRUE)
  )

Map3

Overlaying percentiles

exampleshapes2 <- as(arransimd, "Spatial") %>%
spTransform(CRS("+proj=longlat +datum=WGS84"))

Creating a palate for percentiles

pal2 <- colorNumeric(
  palette = "viridis",
  domain = exampleshapes2$Percentile)

Creating the percentile labels

listlistlist <- paste(datazonelist, exampleshapes2$Percentile, sep=" ") %>%
paste("%", sep="")

Map

m %>% 

#alldatazones  
addPolygons(data=exampleshapes2[exampleshapes2$year == 2004, ], 
            weight = 2, 
            label = listlistlist[29:35],
            group = "2004",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("2004") %>% 
  
addPolygons(data=exampleshapes2[exampleshapes2$year == 2006, ], 
            weight = 2, 
            label = listlistlist[22:28],
            group = "2006",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("2006") %>% 
  
addPolygons(data=exampleshapes2[exampleshapes2$year == 2009, ], 
            weight = 2, 
            label = listlistlist[15:21],
            group = "2009",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("2009") %>% 
  
addPolygons(data=exampleshapes2[exampleshapes2$year == 2012, ], 
            weight = 2, 
            label = listlistlist[8:14],
            group = "2012",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("2012") %>% 

addPolygons(data=exampleshapes2[exampleshapes2$year == 2016, ], 
            weight = 2, 
            label = listlistlist[1:7],
            group = "2016",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("2016") %>% 
  
#cliniccoordinates
addMarkers(
    lng = cliniccoordinates$longitude, lat = cliniccoordinates$latitude,
    label = cliniccoordinates$X,
    labelOptions = labelOptions(noHide = F), group = cliniccoordinates$X) %>%
  hideGroup(cliniccoordinates$X) %>%   

addLegend("bottomleft", pal = pal2, values = exampleshapes2$Percentile,
    title = "SIMD Percentile",
    labFormat = labelFormat(suffix = "%"),
    opacity = 1
  )  %>%  

#Layers control
addLayersControl(
    baseGroups = c("2004", "2006", "2009", "2012", "2016", "Nothing"),
    overlayGroups = c(cliniccoordinates$X),
    options = layersControlOptions(collapsed = TRUE)
  )

Map4 Arran vs. Scotland

Beautiful map, but I have to leave the laptop running overnight to compile it.

#Import UK data
DZBoundaries2016 <- read_sf("../alldata/SG_SIMD_2016")
Scotland2016 <- as(DZBoundaries2016, "Spatial") %>%
spTransform(CRS("+proj=longlat +datum=WGS84"))

DZBoundaries2012 <- read_sf("../alldata/SG_SIMD_2012")
Scotland2012 <- as(DZBoundaries2012, "Spatial") %>%
spTransform(CRS("+proj=longlat +datum=WGS84"))

DZBoundaries2009 <- read_sf("../alldata/SG_SIMD_2009")
Scotland2009 <- as(DZBoundaries2009, "Spatial") %>%
spTransform(CRS("+proj=longlat +datum=WGS84"))

DZBoundaries2006 <- read_sf("../alldata/SG_SIMD_2006")
Scotland2006 <- as(DZBoundaries2006, "Spatial") %>%
spTransform(CRS("+proj=longlat +datum=WGS84"))

DZBoundaries2004 <- read_sf("../alldata/SG_SIMD_2004")
Scotland2004 <- as(DZBoundaries2004, "Spatial") %>%
spTransform(CRS("+proj=longlat +datum=WGS84"))

#Colour Palate
pal2 <- colorNumeric(
  palette = "viridis",
  domain = 0:100)

leaflet() %>% 
  addTiles() %>% 
  setView(-5.227680, 55.582338, zoom = 10) %>% 

addPolygons(data=exampleshapes2[exampleshapes2$year == 2004, ], 
            weight = 2, 
            label = listlistlist[29:35],
            group = "2004",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("2004") %>% 
  
addPolygons(data=Scotland2004, 
            weight = 2, 
            group = "Scotland 2004",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("Scotland 2004") %>% 
  
addPolygons(data=exampleshapes2[exampleshapes2$year == 2006, ], 
            weight = 2, 
            label = listlistlist[22:28],
            group = "2006",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("2006") %>% 

addPolygons(data=Scotland2006, 
            weight = 2, 
            group = "Scotland 2006",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("Scotland 2006") %>% 
  
addPolygons(data=exampleshapes2[exampleshapes2$year == 2009, ], 
            weight = 2, 
            label = listlistlist[15:21],
            group = "2009",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("2009") %>% 

addPolygons(data=Scotland2009, 
            weight = 2, 
            group = "Scotland 2009",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("Scotland 2009") %>% 
  
addPolygons(data=exampleshapes2[exampleshapes2$year == 2012, ], 
            weight = 2, 
            label = listlistlist[8:14],
            group = "2012",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("2012") %>% 
  
addPolygons(data=Scotland2012, 
            weight = 2, 
            group = "Scotland 2012",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("Scotland 2012") %>% 

addPolygons(data=exampleshapes2[exampleshapes2$year == 2016, ], 
            weight = 2, 
            label = listlistlist[1:7],
            group = "2016",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("2016") %>% 

addPolygons(data=Scotland2016, 
            weight = 2, 
            group = "Scotland 2016",
            fillOpacity =0.8,
            color = ~pal2(Percentile),
            highlightOptions = highlightOptions(color = "black", weight = 2,
      bringToFront = TRUE)) %>% 
hideGroup("Scotland 2016") %>% 
  
addMarkers(
    lng = cliniccoordinates$longitude, lat = cliniccoordinates$latitude,
    label = cliniccoordinates$X,
    labelOptions = labelOptions(noHide = F), group = cliniccoordinates$X) %>%
  hideGroup(cliniccoordinates$X) %>%   

addLegend("bottomleft", pal = pal2, values = exampleshapes2$Percentile,
    title = "SIMD Percentile",
    labFormat = labelFormat(suffix = "%"),
    opacity = 1
  )  %>%  

addLayersControl(
    baseGroups = c("2004", "Scotland 2004", "2006", "Scotland 2006", "2009", "Scotland 2009", "2012", "Scotland 2012", "2016", "Scotland 2016", "Nothing"),
    overlayGroups = c(cliniccoordinates$X),
    options = layersControlOptions(collapsed = TRUE)
  )

Map4 Arran vs. Scotland Map

Map4

Map4

Map5

I’ll continue this on a new document so I can knit it to an html website.

Map5.

Map5 Code.

But the development is that I’ve used intersect() on each filtered ‘SG_SIMD_…’ to create the ‘sharedvariables’ vector, so I was able to create a new dataframe of every year with only shared variables, rather than percentile alone which I originally used as an example to practice the maps on (my previous version of ‘arransimd’). The new ‘arranSIMD’ can then have a version of map3 for each shared variable.

Go back.