Hello R Studio Community
I am very new to R and I am trying to create an interactive choropleth map. I have created two maps, one with 2020 data and one with 2016 data and would like to display both using the addLayersControl function. However when I click on the radio button, it only displays data for one year for each map instance.
I'm obviously going wrong somewhere and have looked at similar examples, without success. I would greatly appreciate any guidance as to where I am going wrong please.
Many thanks
Jacqueline
# Load required packages to handle shapefiles, transform the data, import from
# Excel and create the map.
library(SPARQL)
library(dplyr)
library(readxl)
library(leaflet)
library(rgdal)
# Download the Local Authority District (LAD) shapefile.
# NOTE: I store it in a local folder. You have to change that if needed.
download.file("https://opendata.arcgis.com/datasets/fab4feab211c4899b602ecfbfbc420a3_4.zip?outSR=%7B%22wkid%22%3A27700%2C%22latestWkid%22%3A27700%7D",destfile="LAD.zip")
# Unzip this file. You can do it with R (as below), or clicking on the object you downloaded.
system("unzip C:/Users/Jac/Downloads/Local_Authority_Districts_December_2017_Ultra_Generalised_Clipped_Boundaries_in_United_Kingdom_WGS84.zip")
# Read in historical data from Excel file
exceldata <- read_excel('C:/Users/Jac/Documents/BSc Computing Year 4 Pt2/Dissertation/RTest/Maps/SIMDAllTest.xlsx')
# SPARQL query variable to retrieve data zones and their respective SIMD rank values
query1 <- 'PREFIX qb: <http://purl.org/linked-data/cube#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sdmx: <http://purl.org/linked-data/sdmx/2009/concept#>
PREFIX data: <http://statistics.gov.scot/data/>
PREFIX sdmxd: <http://purl.org/linked-data/sdmx/2009/dimension#>
PREFIX mp: <http://statistics.gov.scot/def/measure-properties/>
PREFIX stat: <http://statistics.data.gov.uk/def/statistical-entity#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?dataZone ?SIMDrank
WHERE {
?indicator qb:dataSet data:scottish-index-of-multiple-deprivation;
<http://statistics.gov.scot/def/dimension/simdDomain> <http://statistics.gov.scot/def/concept/simd-domain/simd>;
mp:rank ?SIMDrank;
sdmxd:refPeriod <http://reference.data.gov.uk/id/year/2020> ;
sdmxd:refArea ?area.
?area rdfs:label ?dataZone.
}'
# SPARQL endpoint to retrieve the data
endpoint <- "http://statistics.gov.scot/sparql"
# Assign output of SPARQL query to 'qddata'
qddata <- SPARQL(endpoint, query1)
# Assign results of SPARQL query to data frame 'SIMDrank'
SIMDrank <- qddata$results
# SPARQL query to retrieve data zones, council areas and council area codes
query2 <- 'PREFIX qb: <http://purl.org/linked-data/cube#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sdmx: <http://purl.org/linked-data/sdmx/2009/concept#>
PREFIX data: <http://statistics.gov.scot/data/>
PREFIX sdmxd: <http://purl.org/linked-data/sdmx/2009/dimension#>
PREFIX mp: <http://statistics.gov.scot/def/measure-properties/>
PREFIX stat: <http://statistics.data.gov.uk/def/statistical-entity#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?dataZone ?councilArea ?councilAreaCode
WHERE {
?dz <http://statistics.gov.scot/def/hierarchy/best-fit#council-area> ?ca.
?ca rdfs:label ?councilArea.
?ca <http://publishmydata.com/def/ontology/foi/code> ?councilAreaCode.
?dz rdfs:label ?dataZone.
}'
# Assign output of SPARQL query to 'qddata2'
qddata2 <- SPARQL(endpoint, query2)
# Assign results of SPARQL query to data frame 'geo_lkp'
geo_lkp <- qddata2$results
# Join the 2 data frames to link SIMD to council areas
SIMD_ca_2020 <- inner_join(SIMDrank, geo_lkp, by="dataZone")
# Join the new data frame to the Excel data for 2016
SIMD_ca_2016 <- inner_join(exceldata, geo_lkp, by="dataZone")
# Calculate mean SIMD rank per council area for 2020
SIMD_mean_2020 <- SIMD_ca_2020 %>%
group_by(councilAreaCode, councilArea) %>%
summarise(meanSIMDrank=mean(SIMDrank))
# Calculate mean SIMD rank per council area for 2016
SIMD_mean_2016 <- SIMD_ca_2016 %>%
group_by(councilAreaCode, councilArea) %>%
summarise(meanSIMDrank2=mean(SIMD2016))
# Load shapefile into R as spatial polygons data frame
boundary <- readOGR(dsn="C:/Users/Jac/Documents/BSc Computing Year 4 Pt2/Dissertation/RTest/Maps", layer="Local_Authority_Districts_December_2017_Ultra_Generalised_Clipped_Boundaries_in_United_Kingdom_WGS84")
# Merge spatial polygons data frame with data frame containing mean for 2020
merged_2020 <- merge(boundary, SIMD_mean_2020, by.x = "lad17nm",
by.y = "councilArea", all.x = FALSE)
# Merge spatial polygons data frame with data frame containing mean for 2016
merged_2016 <- merge(boundary, SIMD_mean_2016, by.x = "lad17nm",
by.y = "councilArea", all.x = FALSE)
# Project 2020 data to World Geodetic System 1984 using spTransform to ensure correct plotting
project_2020 <- spTransform(merged_2020,
CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
# Project 2016 data to World Geodetic System 1984 using spTransform to ensure correct plotting
project_2016 <- spTransform(merged_2016,
CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
# Create bins and palette for mean SIMD rank
bins <- c(2000, 2500, 3000, 3500, 4000, 4500, 5000, 6000)
pal2020 <- colorBin("YlOrRd", domain = project_2020$meanSIMDrank, bins = bins)
pal2016 <- colorBin("Blues", domain = project_2016$meanSIMDrank, bins = bins)
# Plot mean 2020 SIMD rank for each council area
map2020 <- leaflet(project_2020) %>%
addProviderTiles("CartoDB.Positron",
options= providerTileOptions(opacity = 0.99)) %>%
addPolygons(fillColor = ~pal2020(meanSIMDrank),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
highlight = highlightOptions(
weight = 2,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
group = "SIMD2020",
label=~paste(project_2020$lad17nm,
round(project_2020$meanSIMDrank)),
labelOptions = labelOptions(textsize = "15px",
direction = "auto")) %>%
addLegend(pal = pal2020,
values = ~meanSIMDrank,
opacity = 0.7,
title = "Mean SIMD Rank 2020",
position = "bottomright")
addLayersControl(map2020,
baseGroups = c("SIMD2016", "SIMD2020"),
position = "bottomleft",
options = layersControlOptions(collapsed = FALSE)
)
# Plot mean 2016 SIMD rank for each council area
map2016 <- leaflet(project_2016) %>%
addProviderTiles("CartoDB.Positron",
options= providerTileOptions(opacity = 0.99)) %>%
addPolygons(fillColor = ~pal2016(meanSIMDrank2),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 07,
highlight = highlightOptions(
weight = 2,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
group = "SIMD2016",
label=~paste(project_2016$lad17nm,
round(project_2016$meanSIMDrank2)),
labelOptions = labelOptions(textsize = "15px",
direction = "auto"))%>%
addLegend(pal = pal2016,
values = ~meanSIMDrank2,
opacity = 0.7,
title = "Mean SIMD Rank 2016",
position = "bottomright")
addLayersControl(map2016,
baseGroups = c("SIMD2016", "SIMD2020"),
position = "bottomleft",
options = layersControlOptions(collapsed = FALSE)
)