knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
#comment out if load_all() hasn't run. if you clear environment pane this fails library(lucabenpkgr) #other dependencies require(plotly) require(tidyr) require(dplyr) require(lubridate) require(forcats) require(grid) require(htmltools) require(gridExtra) #require(mosaic)
data(world_climate)
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))
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" )) }
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
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
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")) ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.