Skip to contents
#comment out if load_all() hasn't run. if you clear environment pane this fails
library(lucabenpkgr)

#other dependencies
require(plotly)
#> Loading required package: plotly
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
require(tidyr)
#> Loading required package: tidyr
require(dplyr)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
require(lubridate)
#> Loading required package: lubridate
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
require(forcats)
#> Loading required package: forcats
require(grid)
#> Loading required package: grid
require(htmltools)
#> Loading required package: htmltools
require(gridExtra)
#> Loading required package: gridExtra
#> Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
#> logical.return = TRUE, : there is no package called 'gridExtra'
#require(mosaic)
data(world_climate)

US CO2 Consumption by Fuel Type

us_co2 <- world_climate %>%
  filter(country_code == 'USA', 
         indicator_code %in% c("EN.ATM.CO2E.SF.KT", 
                               "EN.ATM.CO2E.LF.KT", 
                               "EN.ATM.CO2E.GF.KT")) %>%
   mutate(indicator_code_labelled = fct_recode(indicator_code, 
                                               `Solid Fuel (Coal)` = "EN.ATM.CO2E.SF.KT",
                                               `Liquid Fuel (Petroleum)` = "EN.ATM.CO2E.LF.KT",
                                               `Gaseous Fuel (Natural Gas)` = "EN.ATM.CO2E.GF.KT"))
plot_ly(data=us_co2, 
        x=~year, 
        y=~measure, 
        color=~indicator_code_labelled, 
        type="scatter", 
        mode="lines") %>%
  layout(title = "USA CO2 Consumption - Fuel Outputs",
         yaxis = list(title = "CO2 Emissions (kt)"),
         xaxis = list(title = "Year", tickfont = list(size=10)),
         legend = list(x = 0.8, y = 0.1))

Choropleth Maps

Will just be looking at North and South American countries - this dataset has 3 letter country codes, and the values filtered for below will be applied to a filter in the climate dataset.

data(alpha3codes)
alpha3codes <- alpha3codes %>%
#  filter(region == "Americas") %>%
  select(alpha_3)
alpha3list <-  as.list(as.data.frame(t(alpha3codes)))
world_climate <- world_climate %>%
  filter(country_code %in% alpha3list)
# Display a map for whatever year and indicator 
show_maps <- function(df, id_code, map_title, years, color, b) {
  
     temp_df <- df %>% 
       filter(indicator_code == id_code)
    
     year <- df$year[1]
     
    title <- paste(map_title, year, sep=', ')
    
    
    plot_ly(type='choropleth', locations=temp_df$country_code,
                z=temp_df$measure, text=temp_df$country_name, colorscale=color
            , reversescale = b) %>%
        layout(title = title, geo = list(
            scope = "world",
            showframe = FALSE,
            projection = list(type = 'eckert4'),
            showlakes = TRUE,
            lakecolor = toRGB('white'),
            showocean=TRUE 
          #  oceancolor="LightBlue"
))
    
}

CO2 Gas emissions

years <- list("1997", "2007", "2017")
years <- ymd(years, truncated = 2L)
df1997 <- world_climate %>% filter(year == years[[1]])
df2007 <- world_climate %>% filter(year == years[[2]])
df2017 <- world_climate %>% filter(year == years[[3]])
df_list <- list(df1997, df2007, df2017)
id_code1 = "EN.ATM.GHGT.KT.CE"
map_title1 = "Total greenhouse gas emissions (kt of CO2 equivalent)"
output1 <- tagList(lapply(df_list, show_maps, id_code=id_code1, 
                  map_title=map_title1, years=years, color="Reds", b=F))
output1

Renewable Electricity Output

id_code2 = "EG.FEC.RNEW.ZS"
map_title2 = "Total amount of electricity from renewable energy sources (% total)"
output2 <-tagList(lapply(df_list, show_maps, id_code=id_code2, 
                  map_title=map_title2, years=years, color="Greens", b= T))
output2

Tabular View - Renewable Energy Output

elec_perc <- world_climate %>%
  filter(year %in% years, 
         indicator_code == id_code2) %>%
          pivot_wider(names_from = year, values_from = measure) %>%
          select(country_name, `1997-01-01`, `2007-01-01`, `2017-01-01`) %>%
          filter(!is.na(`1997-01-01`)) %>%
          rename(Country = "country_name",
                 `1997` = `1997-01-01`,
                 `2007` = `2007-01-01`,
                 `2017` = `2017-01-01`)
elec_perc_cond <- elec_perc %>%
   mutate(highlight97 = ifelse(`1997` >= 25.0 , "#90EE90", "white"),
          highlight07 = ifelse(`2007` >= 25.0 , "#90EE90", "white"),
          highlight17 = ifelse(`2017` >= 25.0 , "#90EE90", "white"))
plot_ly(data=elec_perc,
        type='table',
       # columnorder = c(1,2,3,4),
        columnwidth = c(10, 10, 10, 10),
        header = list(
          values = c(names(elec_perc)),
          align = c('left', rep('center', ncol(elec_perc))),
          line = list(width = 1, color = 'black'),
          fill = list(color = 'rgb(45, 112, 230)'),
          font = list(family = "Arial", size = 14, color = "white")
        ),
        cells = list(values=
                       rbind( t(as.matrix(unname(elec_perc)))),
                       align = c('left', rep('center', ncol(elec_perc))),
                      line = list(color = "black", width = 1),
                      fill = list(color = list('rgb(176, 200, 232)',
                                               elec_perc_cond$highlight97,
                                               elec_perc_cond$highlight07,
                                               elec_perc_cond$highlight17)),
                      font = list(family = "Arial", size = 12, color = c("black"))
                     )
        
        )