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

Extract code from app to develop plots.

parent 5dce4049
No related branches found
No related tags found
No related merge requests found
# rsconnect::deployApp('/Users/twr1m15/SotonGitLab/woRkflow/howTo/openGeogAPI', appName = "LAemissions",appTitle = "Hampshire local authority ghg emissions by sector")
# Load libraries ----
library(readxl)
library(ggplot2)
library(plotly)
library(dplyr)
library(reshape2)
library(RColorBrewer)
# Construct colour palette ----
industry_pal <- brewer.pal(n = 8, name = "Greys")[4:8] # industry greys, 5 categories
domestic_pal <- brewer.pal(n = 4, name = "Blues")[2:4] # domestic blues, 3 categories
transport_pal <- brewer.pal(n = 6, name = "Oranges")[2:6] # transport oranges, 5 categories
lulucf_pal <- brewer.pal(n = 9, name = "Greens")[4:9] # lulucf greens, 6 categories
# for details
detailed_pal <- c(industry_pal, domestic_pal, transport_pal, lulucf_pal)
# for totals/outlines
totals_pal <- c(brewer.pal(n = 9, name = "Greys")[8],
brewer.pal(n = 9, name = "Blues")[8],
brewer.pal(n = 9, name = "Oranges")[8],
brewer.pal(n = 9, name = "Greens")[8])
# 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",
"Eastleigh","Isle of Wight","Fareham",
"Gosport","Test Valley","East Hampshire",
"Havant","New Forest","Hart","Basingstoke and Deane")
# Load GHG emissions data ----
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")
download.file(url_to_get, tempf, method = "curl")
dt <- readxl::read_xlsx(tempf, sheet = "Full dataset",skip = 1)
x_min <- min(dt$Year)
x_max <- max(dt$Year)
# Functions ----
lvl_detail <- "high"
filter_detail <- function(lvl_detail = lvl_detail){
if(lvl_detail == "low"){
dt <- dt %>%
filter(Name %in% las_to_load) %>%
select(`CTRY18NM/RGN18NM`,`Second Tier Authority`,Name,
Code,Year,`Industry and Commercial Total`,
`Domestic Total`,`Transport Total`,`LULUCF Net Emissions`)
}
if(lvl_detail == "high"){
dt <- dt %>%
filter(Name %in% las_to_load) %>%
select(`CTRY18NM/RGN18NM`,`Second Tier Authority`,Name,Code,Year,
`A. Industry and Commercial Electricity`,`B. Industry and Commercial Gas`,
`C. Large Industrial Installations`,`D. Industrial and Commercial Other Fuels`,
`E. Agriculture`,
`F. Domestic Electricity`,`G. Domestic Gas`,`H. Domestic 'Other Fuels'`,
`I. Road Transport (A roads)`,`J. Road Transport (Motorways)`,`K. Road Transport (Minor roads)`,
`L. Diesel Railways`,`M. Transport Other`,
`LULUCF Net Emissions`)
}
ghg_emissions <- melt(dt, id.vars = c("CTRY18NM/RGN18NM","Second Tier Authority","Name","Code","Year"))
return(ghg_emissions)
}
# Process data ----
per_capita_dt <- dt %>%
filter(Name %in% las_to_load) %>%
rename(Population = `Population ('000s, mid-year estimate)`) %>%
mutate(Population = Population*1000)
per_cap_fun <- function(x) x*1000/per_capita_dt$Population
per_capita_totals <- data.frame(per_capita_dt[c(1:5,30)], lapply(per_capita_dt[c(11,15,21,28,29,32,33)], per_cap_fun) )
per_capita_detail <- data.frame(per_capita_dt[c(1:5,30)], lapply(per_capita_dt[c(6:10,12:14,16:20,22:27,29,32,33)], per_cap_fun) )
# Note that for hampshire LAs there are no emissions in categories Q or S
# Reshape data
pc_totals_plot <- data.frame(per_capita_totals[c(1:5,7:10)])
pc_totals_plot <- melt(pc_totals_plot, id.vars = c("CTRY18NM.RGN18NM","Second.Tier.Authority","Name","Code","Year"))
pc_details_plot <- data.frame(per_capita_totals[c(1:5,7:10)])
pc_details_plot <- melt(pc_totals_plot, id.vars = c("CTRY18NM.RGN18NM","Second.Tier.Authority","Name","Code","Year"))
# Construct plots ----
dt2 <- dt %>%
filter(Name %in% las_to_load) %>%
rename(Population = `Population ('000s, mid-year estimate)`) %>%
mutate(
Population = Population*1000,
`Industry per capita` = `Industry and Commercial Total`*1000/Population,
`Domestic per capita` = `Domestic Total`*1000/Population,
`Transport per capita` = `Transport Total`*1000/Population,
`LULUCF per capita` = `LULUCF Net Emissions`*1000/Population,
`Total per capita chk` = `Industry per capita` + `Domestic per capita` +
`Transport per capita` + `LULUCF per capita`,
`Total per capita` = `Grand Total`*1000/Population
)
dt3 <- dt %>%
filter(Name %in% las_to_load) %>%
rename(Population = `Population ('000s, mid-year estimate)`) %>%
mutate(
Population = Population*1000,
`Industry (elec) per capita` = `A. Industry and Commercial Electricity`*1000/Population,
`Industry (gas) per capita` = `B. Industry and Commercial Gas`*1000/Population,
`Industry (lge) per capita` = `C. Large Industrial Installations`*1000/Population,
`Industry (other fuels) per capita` = `D. Industrial and Commercial Other Fuels`*1000/Population,
`Domestic (elec) per capita` = `F. Domestic Electricity`*1000/Population,
`Domestic (gas) per capita` = `G. Domestic Gas`*1000/Population,
`Domestic (other fuels) per capita` = `H. Domestic 'Other Fuels'`*1000/Population,
`Transport (A roads) per capita` = `I. Road Transport (A roads)`*1000/Population,
`Transport (Motorways) per capita` = `J. Road Transport (Motorways)`*1000/Population,
`Transport (Minor roads) per capita` = `K. Road Transport (Minor roads)`*1000/Population,
`Transport (Diesel rail) per capita` = `L. Diesel Railways`*1000/Population,
`Transport (other) per capita` = `M. Transport Other`*1000/Population
)
# ghg_emissions <- filter_detail(lvl_detail = "low")
ghg_emissions <- filter_detail(lvl_detail = "high")
rm(dt)
## Subset data - Southampton by default
ghg_subset <- function(auth_area = "Southampton"){
ghg_emissions_sub <- ghg_emissions %>%
filter(ghg_emissions$Name %in% auth_area)
# add filter for categories with input$
}
ghg_emissions_sub <- ghg_subset()
# Plots
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment