Tables

Author

Drew Clayson

Data Gathering

I am doing my data based on the G20 members to keep a concise table. I filter the dates down to ensure that there are no missing values (some data have not been updated since 2015).

library(wbstats)
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
✓ ggplot2 3.3.5     ✓ purrr   0.3.4
✓ tibble  3.1.6     ✓ dplyr   1.0.8
✓ tidyr   1.2.0     ✓ stringr 1.4.0
✓ readr   2.1.2     ✓ forcats 0.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(knitr)
library(kableExtra)

Attaching package: 'kableExtra'
The following object is masked from 'package:dplyr':

    group_rows
raw_data <- wb_data(country = c("ARG", "AUS", "BRA", "CAN", "CHN", "FRA", "DEU", "IND", "IDN", "ITA", "KOR", "JPN", "MEX", "RUS", "SAU", "ZAF", "TUR", "GBR", "USA", "EU"), indicator = c("NY.GDP.MKTP.KD", "NY.GDP.PCAP.KD.ZG", "EN.ATM.CO2E.KT", "EG.ELC.RNEW.ZS", "NY.GNS.ICTR.ZS"))
clean_data <- raw_data %>% filter(date < 2016) %>% filter(date > 1999) %>% select(c(3:9))
colnames(clean_data) <- c("Country", "Date", "Percent Renewable", "CO2", "GDP(billions)", "GDP Per Capita Growth", "Savings Rate")
clean_data$`GDP(billions)` <- clean_data$`GDP(billions)`/1000000000

For my first table, I will be making a relatively simple table with a few added flairs pointing out interesting information. I understand that it might be slightly redundant to simply highlight negative numbers in red, however I think it makes it much easier for readers to glean simple information quickly. Furthermore I chose to highlight relatively large savings rates in green and found that they tend to all be from Asia, which is perhaps an interesting insight!

clean_data$`GDP Per Capita Growth` <- ifelse(
  clean_data$`GDP Per Capita Growth` < 0,
  cell_spec(round(clean_data$`GDP Per Capita Growth`,digits = 2), color = "red", bold = TRUE),
  cell_spec(round(clean_data$`GDP Per Capita Growth`,digits = 2))
)
clean_data$`Savings Rate` <- ifelse(
  clean_data$`Savings Rate`>30,
  cell_spec(round(clean_data$`Savings Rate`,digits = 2), color = "green"),
  cell_spec(round(clean_data$`Savings Rate`, digits = 2))
)

tbl <- clean_data %>%
  ungroup() %>%
  filter(Date == 2015) %>%
  arrange(desc(`GDP(billions)`)) %>%
  select(Country, `GDP(billions)`, `GDP Per Capita Growth`, `Savings Rate`, CO2, `Percent Renewable`) %>%
  kbl(escape = FALSE,
    digits = 2) %>%
  add_header_above(c(" ", "Economic Data " = 3, "Environmental Data" = 2))
tbl %>%
  kable_material(lightable_options = c("striped", "hover", "")) %>%
  htmltools::HTML() %>%
  shiny::div() %>%
  sparkline::spk_add_deps()
Economic Data
Environmental Data
Country GDP(billions) GDP Per Capita Growth Savings Rate CO2 Percent Renewable
United States 18206.02 1.95 20.24 4990710 13.23
European Union 13551.93 2.09 23.94 2895700 29.72
China 11061.55 6.42 45.42 9861100 23.93
Japan 4444.93 1.67 28.18 1179440 15.98
Germany 3357.59 0.62 28.33 742310 29.23
United Kingdom 2956.57 1.81 12.53 401080 24.84
France 2439.19 0.75 22.25 311300 15.86
India 2103.59 6.8 32.45 2158020 15.34
Italy 1836.64 0.88 18.52 337860 38.68
Brazil 1802.21 -4.35 14.31 485340 73.97
Canada 1556.51 -0.09 19.57 558700 63.01
Korea, Rep. 1465.77 2.27 36.35 607830 1.89
Russian Federation 1363.48 -2.18 26.37 1592560 15.86
Australia 1350.53 0.71 22.7 377800 13.64
Mexico 1171.87 2.02 21.76 471630 15.39
Turkiye 864.32 4.33 25.21 351590 31.96
Indonesia 860.85 3.56 30.12 488550 10.65
Saudi Arabia 654.27 1.48 25.5 565190 0.00
Argentina 594.75 1.63 14.31 185550 28.14
South Africa 346.71 -0.22 14.29 424810 2.26

From here, I will try to make use of the time series data using sparkline. I would really only like to include time series of GDP, CO2, and Percent Renewable. I could do time series of the other data, however I don’t believe it to be particularly interesting. The chart will still use the same data as above so all color highlights remain the same, which is nice functionality.

library(sparkline)
# Create similar table to the one above,  however mutating to add 3 more variables
tbl2 <- clean_data %>%
  filter(Date == 2015) %>%
  arrange(desc(`GDP(billions)`)) %>%
  select(Country, `GDP(billions)`, `GDP Per Capita Growth`, `Savings Rate`, CO2, `Percent Renewable`) %>%
  mutate(plt_GDP = NA, plt_CO2 = NA, plt_PercentRenewable = NA) %>% as.data.frame()
# Generate plots using a for loop
for(i in tbl2$Country){
  # Data for GDP
  country_GDP <- clean_data %>%
    filter(Country==i) %>%
    arrange(Date) %>%
    pull(`GDP(billions)`)
  tbl2[which(tbl2$Country == i), "plt_GDP"] <- spk_chr(country_GDP)
  # Data for CO2
  country_CO2 <- clean_data %>%
    filter(Country==i) %>%
    arrange(Date) %>%
    pull(CO2)
  tbl2[which(tbl2$Country == i), "plt_CO2"] <- spk_chr(country_CO2)
  # Data for Renewables
  country_PercentRenewable <- clean_data %>%
    filter(Country==i) %>%
    arrange(Date) %>%
    pull(`Percent Renewable`)
  tbl2[which(tbl2$Country == i), "plt_PercentRenewable"] <- spk_chr(country_PercentRenewable)
}
tbl2_full <- tbl2 %>%
  select(Country, `GDP(billions)`, plt_GDP, `GDP Per Capita Growth`, `Savings Rate`, CO2, plt_CO2, `Percent Renewable`, plt_PercentRenewable) %>%
  kbl(escape = FALSE,
    col.names = c("Country",
                    "GDP (billions)",
                    "GDP Trend",
                    "GDP per Capita Growth",
                    "Savings Rate",
                    "CO2 Emissions",
                    "Emission Trend",
                    "Percent Renewable",
                    "Renewability Trend"
                    ),
      digits = 2) %>%
  add_header_above(c(" ", "GDP" = 3, " ", "Environmentals" = 4)) %>%
  kable_styling(font_size=28, fixed_thead = TRUE)
tbl2_full %>%
  htmltools::HTML() %>%
  shiny::div() %>%
  sparkline::spk_add_deps()
GDP
Environmentals
Country GDP (billions) GDP Trend GDP per Capita Growth Savings Rate CO2 Emissions Emission Trend Percent Renewable Renewability Trend
United States 18206.02 1.95 20.24 4990710 13.23
European Union 13551.93 2.09 23.94 2895700 29.72
China 11061.55 6.42 45.42 9861100 23.93
Japan 4444.93 1.67 28.18 1179440 15.98
Germany 3357.59 0.62 28.33 742310 29.23
United Kingdom 2956.57 1.81 12.53 401080 24.84
France 2439.19 0.75 22.25 311300 15.86
India 2103.59 6.8 32.45 2158020 15.34
Italy 1836.64 0.88 18.52 337860 38.68
Brazil 1802.21 -4.35 14.31 485340 73.97
Canada 1556.51 -0.09 19.57 558700 63.01
Korea, Rep. 1465.77 2.27 36.35 607830 1.89
Russian Federation 1363.48 -2.18 26.37 1592560 15.86
Australia 1350.53 0.71 22.7 377800 13.64
Mexico 1171.87 2.02 21.76 471630 15.39
Turkiye 864.32 4.33 25.21 351590 31.96
Indonesia 860.85 3.56 30.12 488550 10.65
Saudi Arabia 654.27 1.48 25.5 565190 0.00
Argentina 594.75 1.63 14.31 185550 28.14
South Africa 346.71 -0.22 14.29 424810 2.26