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 ‘Map5_Code.Rmd’.. Or directly from github https://github.com/fergustaylor/Arran (in /docs).

///

Mapping

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

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) 

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)

Map

Overlaying percentiles

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

Convert to function

functionmap <- function(argument, argument2) 
{
pal2 <- colorNumeric(palette = "magma",
  domain = argument)

listlistlist <- paste(datazonelist, argument, sep=" ")

m %>% 

#alldatazones  
addPolygons(data=exampleshapes2[exampleshapes2$year == 2004, ], 
            weight = 2, 
            label = listlistlist[29:35],
            group = "2004",
            fillOpacity =0.8,
            color = ~pal2(argument[29:35]),
            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(argument[22:28]),
            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(argument[15:21]),
            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(argument[8:14]),
            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(argument[1:7]),
            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 = argument,
    title = argument2,
    opacity = 1
  )  %>%  

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

Each map

See these maps.

map51 <- functionmap(exampleshapes2$Rank, "Rank")
saveWidget(map51, file="map51.html")

map54 <- functionmap(exampleshapes2$Vigintile, "Vigintile")
saveWidget(map54, file="map54.html")
map55 <- functionmap(exampleshapes2$Percentile, "Percentile")
saveWidget(map55, file="map55.html")
map56 <- functionmap(exampleshapes2$IncRate, "IncRate")
saveWidget(map56, file="map56.html")
map57 <- functionmap(exampleshapes2$IncRank, "IncRank")
saveWidget(map57, file="map57.html")
map58 <- functionmap(exampleshapes2$EmpRate, "EmpRate")
saveWidget(map58, file="map58.html")
map59 <- functionmap(exampleshapes2$HlthRank, "HlthRank")
saveWidget(map59, file="map59.html")
map510 <- functionmap(exampleshapes2$EduRank, "EduRank")
saveWidget(map510, file="map510.html")
map511 <- functionmap(exampleshapes2$GAccRank, "GAccRank")
saveWidget(map511, file="map511.html")
map512 <- functionmap(exampleshapes2$HouseRank, "HouseRank")
saveWidget(map512, file="map512.html")
functionmap2 <- function(argument, argument2, argument3) 
{
listlistlist <- paste(datazonelist, argument, sep=" ")

m %>% 

#alldatazones  
addPolygons(data=exampleshapes2[exampleshapes2$year == 2004, ], 
            weight = 2, 
            label = listlistlist[29:35],
            group = "2004",
            fillOpacity =0.8,
            color = ~argument3(argument[29:35]),
            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 = ~argument3(argument[22:28]),
            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 = ~argument3(argument[15:21]),
            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 = ~argument3(argument[8:14]),
            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 = ~argument3(argument[1:7]),
            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 = argument3, values = argument,
    title = argument2,
    opacity = 1
  )  %>%  

#Layers control
addLayersControl(
    baseGroups = c("2004", "2006", "2009", "2012", "2016", "Nothing"),
    overlayGroups = c(cliniccoordinates$X),
    options = layersControlOptions(collapsed = TRUE)
  )
}
pal3 <- colorBin(palette = "magma",
  domain = 0:5, bins = 5)

pal4 <- colorBin(palette = "magma",
  domain = 0:10, bins = 10)
map52 <- functionmap2(exampleshapes2$Quintile, "Quintile", pal3)
saveWidget(map52, file="map52.html")
map53 <- functionmap2(exampleshapes2$Decile, "Decile", pal4)
saveWidget(map53, file="map53.html")

See these maps.

Go back.