From 3f8ff2ba2a0d80dc0b3cada6d5aa9784ca02520d Mon Sep 17 00:00:00 2001 From: Thomas Rushby <t.w.rushby@soton.ac.uk> Date: Wed, 30 Sep 2020 16:58:16 +0100 Subject: [PATCH] Tweaking plot and add by local auth. --- howTo/openGeogAPI/local_auth_ghg_plots.R | 111 ++++++++++++++++++++--- 1 file changed, 98 insertions(+), 13 deletions(-) diff --git a/howTo/openGeogAPI/local_auth_ghg_plots.R b/howTo/openGeogAPI/local_auth_ghg_plots.R index c11f5f9..563284e 100644 --- a/howTo/openGeogAPI/local_auth_ghg_plots.R +++ b/howTo/openGeogAPI/local_auth_ghg_plots.R @@ -23,7 +23,7 @@ detailed_pal <- c(industry_pal, domestic_pal, transport_pal, lulucf_pal) 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]) + brewer.pal(n = 9, name = "Greens")[7:8]) # List local authority areas to load # These used to filter emissions data @@ -91,18 +91,110 @@ per_capita_totals <- data.frame(per_capita_dt[c(1:5,30)], lapply(per_capita_dt[c 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 +# Calculate emissions and removals for LULUCF separately +per_capita_lulucf <- pc_detail_plot %>% + filter(grepl("LULUCF", variable)) %>% + group_by(Name,Year) %>% + summarise( + LULUCF.emissions = sum(value[value > 0]), + LULUCF.removals = sum(value[value < 0])) -pc_totals_plot <- data.frame(per_capita_totals[c(1:5,7:10)]) +per_capita_totals <- left_join(per_capita_totals,per_capita_lulucf, by = c("Name","Year")) + +# Create plot tables ---- + +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")) -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")) +pc_detail_plot <- data.frame(per_capita_detail[c(1:5,7:25)]) +pc_detail_plot <- melt(pc_detail_plot, id.vars = c("CTRY18NM.RGN18NM","Second.Tier.Authority","Name","Code","Year")) +pc_general_plot <- data.frame(per_capita_totals[c(3,5,11:13)]) -# Construct plots ---- +## Subset data - Southampton by default +auth_area <- "New Forest" +ghg_subset <- function(dt, auth_area = "Southampton"){ + dt <- dt %>% + filter(dt$Name %in% auth_area) + # add filter for categories with input$ +} +# Construct plots ---- + +plotCaption = paste0("Emissions data: Department for Business, Energy & Industrial Strategy", + "\nUK local authority and regional carbon dioxide emissions national statistics: 2005 to 2018", + "\nVisualisation: t.w.rushby@soton.ac.uk | energy.soton.ac.uk") + +plotTitle = paste0("Greenhouse gas emissions by source: ",auth_area) + +plot_data1 <- ghg_subset(pc_detail_plot, auth_area = auth_area) +plot_data2 <- ghg_subset(pc_totals_plot, auth_area = auth_area) + +ggplot() + + geom_col(data = plot_data1, aes(x = Year, y = value, fill = variable), position = "stack") + + geom_col(data = plot_data2, aes(x = Year, y = value, colour = variable), fill = "none", position = "stack") + + geom_hline(yintercept=0, lwd=0.4, colour="black", linetype = "dashed") + + coord_cartesian(ylim = c(-1,15)) + + scale_y_continuous(labels=abs) + + scale_x_continuous(breaks = 2005:2018) + + scale_color_manual(values = totals_pal, guide = FALSE) + + scale_fill_manual(values = detailed_pal) + + labs(x = "Year", + y = "Emissions per capita, tCO2", + fill = "Source", + title = plotTitle, + caption = plotCaption) + + theme(legend.position = "right") + + theme_classic() + +plotly::ggplotly(plot) + + +# By local authority + +plot_year <- 2005 + +plot_data1 <- pc_detail_plot %>% filter(Year == plot_year) +plot_data2 <- pc_totals_plot %>% filter(Year == plot_year) + +plotTitle = paste0("Greenhouse gas emissions by sector for Hampshire local authorities: ",plot_year) + +ggplot() + + geom_col(data = plot_data1, aes(x = Name, y = value, fill = variable), position = "stack") + + geom_col(data = plot_data2, aes(x = Name, y = value, colour = variable), fill = "none", position = "stack") + + geom_hline(yintercept=0, lwd=0.4, colour="black", linetype = "dashed") + + coord_cartesian(xlim = c(-1,8)) + + #scale_y_continuous(labels=abs) + + #scale_x_continuous(breaks = 2005:2018) + + scale_color_manual(values = totals_pal, guide = FALSE) + + scale_fill_manual(values = detailed_pal) + + coord_flip() + + labs(x = "Local authority", + y = "Emissions per capita, tCO2", + fill = "Source", + title = plotTitle, + caption = plotCaption) + + theme(legend.position = "none") + + theme_classic() + +ggplot() + + geom_col(data = plot_data1, aes(x = Name, y = value, fill = variable), position = "stack") + + geom_col(data = plot_data2, aes(x = Name, y = value, colour = variable), fill = "none", position = "stack") + + geom_hline(yintercept=0, lwd=0.4, colour="black", linetype = "dashed") + + coord_cartesian(xlim = c(-1,8)) + + #scale_y_continuous(labels=abs) + + #scale_x_continuous(breaks = 2005:2018) + + scale_color_manual(values = totals_pal, guide = FALSE) + + scale_fill_manual(values = detailed_pal) + + coord_flip() + + labs(x = "Local authority", + y = "Emissions per capita, tCO2", + fill = "Source", + title = plotTitle, + caption = plotCaption) + + theme(legend.position = "none") + + theme_classic() @@ -148,14 +240,7 @@ dt3 <- dt %>% 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 -- GitLab