From 7de3a4644b9688ac54cacdc9602c764f4d6e0782 Mon Sep 17 00:00:00 2001 From: Thomas Rushby <t.w.rushby@soton.ac.uk> Date: Thu, 1 Oct 2020 12:28:36 +0100 Subject: [PATCH] Finish Hampshire average and tidying up. --- howTo/openGeogAPI/local_auth_ghg_plots.R | 78 +++++------------------- 1 file changed, 16 insertions(+), 62 deletions(-) diff --git a/howTo/openGeogAPI/local_auth_ghg_plots.R b/howTo/openGeogAPI/local_auth_ghg_plots.R index 1fed925..26acc46 100644 --- a/howTo/openGeogAPI/local_auth_ghg_plots.R +++ b/howTo/openGeogAPI/local_auth_ghg_plots.R @@ -42,11 +42,7 @@ 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 ---- +# Functions (REDUNDANT) ---- lvl_detail <- "high" @@ -79,19 +75,26 @@ filter_detail <- function(lvl_detail = lvl_detail){ # Process data ---- - +# Filter local authorities and correct population per_capita_dt <- dt %>% filter(Name %in% las_to_load) %>% rename(Population = `Population ('000s, mid-year estimate)`) %>% mutate(Population = Population*1000) +# Add Hampshire totals (sum of all las in las_to_load) to calculate averages across las total_hampshire_dt <- dt %>% filter(Name %in% las_to_load) %>% + rename(Population = `Population ('000s, mid-year estimate)`) %>% + mutate(Population = Population*1000) %>% group_by(Year) %>% summarise_if(is.numeric, sum, na.rm = TRUE) %>% - mutate(Name = "Hampshire") - + mutate(Name = " Hampshire (Average)", + `CTRY18NM/RGN18NM` = "South East", + `Second Tier Authority` = "Hampshire", + Code = "E00000000") %>% + select(`CTRY18NM/RGN18NM`,`Second Tier Authority`,Name,Code,Year,everything()) +per_capita_dt <- rbind(per_capita_dt,total_hampshire_dt) 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) ) @@ -119,8 +122,10 @@ pc_totals_plot <- data.frame(per_capita_totals[c(1:5,7:9,14,15)]) pc_totals_plot <- melt(pc_totals_plot, id.vars = c("CTRY18NM.RGN18NM","Second.Tier.Authority","Name","Code","Year")) -## Subset data - Southampton by default -auth_area <- "New Forest" +## Subset data - Hampshire by default +las_to_load <- c(las_to_load, " Hampshire (Average)") # add Hampshire average to list to loop over + +auth_area <- " Hampshire (Average)" # Use to set auth_area manually (outside of loop) ghg_subset <- function(dt, auth_area = "Southampton"){ dt <- dt %>% @@ -171,9 +176,6 @@ i <- i+1 print(plotNames) -plotly::ggplotly(plot) - - # By local authority years_to_plot <- c(2005,2018) @@ -204,7 +206,7 @@ for(plot_year in years_to_plot) { theme(legend.position = "none") + theme_classic() - ggsave(paste0(here::here(),"/howTo/openGeogAPI/plots/",plotName,".png"), dpi = 150, width = 12, height = 6, units = "in") + ggsave(paste0(here::here(),"/howTo/openGeogAPI/plots/",plotName,"_hants.png"), dpi = 150, width = 12, height = 6, units = "in") } @@ -227,51 +229,3 @@ ggplot() + theme_classic() - - - - -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) - - - -# Plots - - -- GitLab