Skip to content
Snippets Groups Projects
Commit 5dce4049 authored by Tom Rushby's avatar Tom Rushby
Browse files

Tidy up.

parent 9e3c114e
No related branches found
No related tags found
No related merge requests found
# Load libraries ----
library(readxl)
library(ggplot2) library(ggplot2)
library(sf) library(sf)
library(htmltools) library(htmltools)
...@@ -8,6 +9,9 @@ library(plotly) ...@@ -8,6 +9,9 @@ library(plotly)
library(dplyr) library(dplyr)
library(reshape2) library(reshape2)
# List local authority areas to load
# These used to filter emissions data
# and construct Open Geog API query (geo_query ... to do)
las_to_load <- c("Southampton","Portsmouth","Winchester", las_to_load <- c("Southampton","Portsmouth","Winchester",
"Eastleigh","Isle of Wight","Fareham", "Eastleigh","Isle of Wight","Fareham",
"Gosport","Test Valley","East Hampshire", "Gosport","Test Valley","East Hampshire",
...@@ -15,8 +19,6 @@ las_to_load <- c("Southampton","Portsmouth","Winchester", ...@@ -15,8 +19,6 @@ las_to_load <- c("Southampton","Portsmouth","Winchester",
# Load GHG emissions data ---- # Load GHG emissions data ----
library(readxl)
url_to_get <- "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/894787/2005-18-uk-local-regional-co2-emissions.xlsx" url_to_get <- "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/894787/2005-18-uk-local-regional-co2-emissions.xlsx"
tempf <- tempfile(fileext = ".xlsx") tempf <- tempfile(fileext = ".xlsx")
...@@ -24,6 +26,30 @@ download.file(url_to_get, tempf, method = "curl") ...@@ -24,6 +26,30 @@ download.file(url_to_get, tempf, method = "curl")
dt <- readxl::read_xlsx(tempf, sheet = "Full dataset",skip = 1) dt <- readxl::read_xlsx(tempf, sheet = "Full dataset",skip = 1)
x_min <- min(dt$Year)
x_max <- max(dt$Year)
# Load LA geography ----
# URL as API query - sometimes we don't want all boundaries
geo_query <- "https://ons-inspire.esriuk.com/arcgis/rest/services/Administrative_Boundaries/Local_Authority_Districts_December_2018_Boundaries_UK_BGC/MapServer/0/query?where=lad18nm%20IN%20(%27Southampton%27,%27Portsmouth%27,%27Winchester%27,%27Eastleigh%27,%27Isle%20of%20Wight%27,%27Fareham%27,%27Gosport%27,%27Test%20Valley%27,%27East%20Hampshire%27,%27Havant%27,%27New%20Forest%27,%27Hart%27,%27Basingstoke%20and%20Deane%27)&outFields=lad18cd,lad18nm,long,lat&outSR=4326&f=geojson"
message("Loading LA geometry from ONS Open Geography API")
sf_data <- st_read(geo_query)
#plot(st_geometry(sf_data))
# Useful lookup spatial reference for CRS
# https://spatialreference.org/ref/epsg/27700/
st_coord_sys <- st_crs(sf_data) # check coord system
st_coord_sys # current coord system EPSG: 4326 (is what leaflet wants - good)
# transform the coord system if required
if(st_coord_sys$epsg != 4326){
sf_data <- st_transform(sf_data, "+proj=longlat +datum=WGS84")
}
# Functions ----
lvl_detail <- "high" lvl_detail <- "high"
filter_detail <- function(lvl_detail = lvl_detail){ filter_detail <- function(lvl_detail = lvl_detail){
...@@ -53,28 +79,10 @@ filter_detail <- function(lvl_detail = lvl_detail){ ...@@ -53,28 +79,10 @@ filter_detail <- function(lvl_detail = lvl_detail){
return(ghg_emissions) return(ghg_emissions)
} }
ghg_emissions <- filter_detail(lvl_detail = "high") ghg_emissions <- filter_detail(lvl_detail = "low")
# ghg_emissions_high <- filter_detail(lvl_detail = "high")
rm(dt) rm(dt)
# Load LA geography ----
# URL as API query - sometimes we don't want all boundaries
geo_query <- "https://ons-inspire.esriuk.com/arcgis/rest/services/Administrative_Boundaries/Local_Authority_Districts_December_2018_Boundaries_UK_BGC/MapServer/0/query?where=lad18nm%20IN%20(%27Southampton%27,%27Portsmouth%27,%27Winchester%27,%27Eastleigh%27,%27Isle%20of%20Wight%27,%27Fareham%27,%27Gosport%27,%27Test%20Valley%27,%27East%20Hampshire%27,%27Havant%27,%27New%20Forest%27,%27Hart%27,%27Basingstoke%20and%20Deane%27)&outFields=lad18cd,lad18nm,long,lat&outSR=4326&f=geojson"
message("Loading LA geometry from ONS Open Geography API")
sf_data <- st_read(geo_query)
#plot(st_geometry(sf_data))
# Useful lookup spatial reference for CRS
# https://spatialreference.org/ref/epsg/27700/
st_coord_sys <- st_crs(sf_data) # check coord system
st_coord_sys # current coord system EPSG: 4326 (is what leaflet wants - good)
# transform the coord system if required
if(st_coord_sys$epsg != 4326){
sf_data <- st_transform(sf_data, "+proj=longlat +datum=WGS84")
}
# Create map (leaflet) ---- # Create map (leaflet) ----
# create popup first (requires htmltools) # create popup first (requires htmltools)
...@@ -166,12 +174,15 @@ server <- function(input, output, session) { ...@@ -166,12 +174,15 @@ server <- function(input, output, session) {
ggplot(ghg_subset(auth_area = input$authmap_shape_click$id), aes(x = Year, y = value)) + ggplot(ghg_subset(auth_area = input$authmap_shape_click$id), aes(x = Year, y = value)) +
geom_col(aes(fill = variable), position = "stack") + geom_col(aes(fill = variable), position = "stack") +
scale_x_continuous(limits = c(x_min-1,x_max+1), breaks = x_min:x_max) +
#scale_y_continuous(limits = c(y_min,y_max)) +
labs(x = "Year", labs(x = "Year",
y = expression("tCO"[2]), y = expression("tCO"[2]),
fill = "Sector", fill = "Sector",
title = plotTitle, title = plotTitle,
caption = plotCaption) + caption = plotCaption) +
theme(legend.position = "bottom") theme(legend.position = "right") +
theme_classic()
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment