diff --git a/howTo/openGeogAPI/app.R b/howTo/openGeogAPI/app.R index 4a4ce77b21376fae90feeddfffa3551bff2fa235..6abe7f604db24ada4779af66a887978ece1cffc3 100644 --- a/howTo/openGeogAPI/app.R +++ b/howTo/openGeogAPI/app.R @@ -5,6 +5,8 @@ library(sf) library(htmltools) library(leaflet) library(plotly) +library(dplyr) +library(reshape2) las_to_load <- c("Southampton","Portsmouth","Winchester", "Eastleigh","Isle of Wight","Fareham", @@ -22,14 +24,36 @@ download.file(url_to_get, tempf, method = "curl") dt <- readxl::read_xlsx(tempf, sheet = "Full dataset",skip = 1) -head(dt) +lvl_detail <- "high" -library(dplyr) -dt <- dt %>% - filter(Name %in% las_to_load) +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) +} -library(reshape2) -ghg_emissions <- melt(dt, id.vars = c("CTRY18NM/RGN18NM","Second Tier Authority","Name","Code","Year")) +ghg_emissions <- filter_detail(lvl_detail = "low") rm(dt) # Load LA geography ---- @@ -60,30 +84,31 @@ sf_data$popup_text <- '<br/>', 'Local authority: ', '<b>', sf_data$lad18nm, '</b>', ' ') %>% lapply(htmltools::HTML) -## Subset data -area <- "Hart" - -ghg_emissions_sub <- ghg_emissions %>% - filter(ghg_emissions$Name %in% area) +## 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$ +} -# area <- input$map_shape_click$lad18nm -#area <- "Hart" +ghg_emissions_sub <- ghg_subset() +# App layout ---- ui <- fluidPage( - titlePanel("Hello Shiny!"), + titlePanel("Hampshire local authority ghg emission visualisation"), - verticalLayout( - - wellPanel(textOutput("selected_auth")), - + fluidRow( - mainPanel(leafletOutput("authmap", width = "100%", height = "600px")), + column(6, + wellPanel("Click map to select local authority"), + leafletOutput("authmap",height = "500px")), - - plotOutput("plot", width = "100%", height = "600px") + column(6, + wellPanel(textOutput("selected_auth")), + plotOutput("plot", height = "500px")) ) @@ -117,7 +142,6 @@ server <- function(input, output, session) { observeEvent( - ## the sgmap2 needs to match the name of the map you're outputting above input$authmap_shape_click, { click <- input$authmap_shape_click @@ -134,8 +158,20 @@ server <- function(input, output, session) { output$plot <- renderPlot({ - ggplot(ghg_emissions_sub, aes(x = Year, y = value)) + - geom_col(aes(fill = variable), position = "stack") + plotCaption = paste0("Emissions data: Department for Business, Energy & Industrial Strategy", + "\nLocal authority boundary data (2018): ONS Open Geography Portal", + "\nVisualisation: rushby.shinyapps.io/LAemissions") + + plotTitle = paste0("Greenhouse gas emissions by sector for ",input$authmap_shape_click$id) + + ggplot(ghg_subset(auth_area = input$authmap_shape_click$id), aes(x = Year, y = value)) + + geom_col(aes(fill = variable), position = "stack") + + labs(x = "Year", + y = expression("tCO"[2]), + fill = "Sector", + title = plotTitle, + caption = plotCaption) + + theme(legend.position = "bottom") })