The Global Economic Output and the Carbon Consequence

Sources: Gapminder and temp_carbon Datasets

These plots show the correlation between global production output, fossil carbon emissions, CO2 accumulation in the atmosphere, and global average temperature rise due to a greenhouse effect. Since global production is exclusively caused by human activity, so is fossil carbon emission, and so is temperature rise.

Install libraries

#install.packages("dslabs")
library("dslabs")
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.4     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(grid)
library(cowplot)

Manipulate data, identify a subset of variables and calculate the total GDP by world region and year, in trillion dollars

gapminder2 <- na.omit(gapminder[gapminder$gdp != 0,])      # filter rows with GDP=0 or NA
# Group by region and year and select Infant_mortality, Life_expectancy, Fertility, Population, and Total GDP
gdp_year <- group_by(gapminder2, region, year) %>% summarise(infant=median(infant_mortality), life=median(life_expectancy), fertility=mean(fertility), population=sum(population), totgdp = sum(gdp)/10^12)
## `summarise()` regrouping output by 'region' (override with `.groups` argument)
# Plot all regions
pgdp<- ggplot(gdp_year, aes (year, totgdp)) + 
  geom_line(aes(color=region)) +
  geom_point(size=1.5, aes(color=region, shape=region)) +
  labs (title="GDP Rises Fast Around the Globe",
        subtitle = "Esp. North America, East Asia, and West Europe", 
        x="Year", y="Annual GDP, $Trillion", caption="Source: Gapminder Dataset") +
  scale_shape_manual(name="Region", 
        values= c(1,2,5,6,7,21,11,12,21,22,24,16,15,20,25,18,0,17,14,13,9,19)) +
  scale_color_manual(name="Region", values = colorRampPalette(brewer.pal(8,"Accent"))(22)) +
  theme(legend.position="bottom", legend.title = element_blank())
pgdp

For clarity, separate the top six regions

# Filter the rows with only the six regions and plot
gdp_year2 <- filter(gdp_year, region %in% c("Northern America", "Eastern Asia", "Western Europe", "Northern Europe", "Southern Europe", "South America"))
# Plot only the six regions
pgdp2<- ggplot(gdp_year2, aes (year, totgdp)) + 
  geom_line(aes(color=region)) +
  geom_point(size=1.5, aes(color=region, shape=region)) +
  labs (title="GDP Rises Fast Around the Globe",
        subtitle = "Esp. North America, East Asia, and West Europe", 
        x="Year", y="Annual GDP, $Trillion", caption="Source: Gapminder Dataset") +    
  scale_shape_manual(name="Region", values= c(15,16,20,18,17,19)) + 
  scale_color_brewer(name="Region", palette = "Dark2") +
  theme_bw() +
  theme(legend.position="bottom", legend.title = element_blank())
pgdp2

Explore the correlation between global GDP, carbon emissions and temperature rise

Join the Gapminder and temp_carbon datasets by year

Express carbon emissions by year in GigaTon (Billion Metric Tons) of CO2-equivalent

Plot each variable to clearly see trends and correlations and try a facet_wrap with all three variables

# Joining the two datasets; notice that left_join automatically filters from temp_carbon only the years available in Gapminder, not requiring filter()

mindcarbon <- left_join(gapminder2, temp_carbon, by = "year") %>%
  mutate(carbon = carbon_emissions/10^3, temp_rise=temp_anomaly)
# Plot each variable and the facet_wrap, in exploratory mode
gdp_year3 <- group_by(mindcarbon, year) %>% summarise(totgdp = sum(gdp)/10^12, carbon, temp_rise)
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
ggplot(gdp_year3, aes (year, totgdp)) +  geom_point()

ggplot(gdp_year3, aes (year, carbon)) +  geom_point()

ggplot(gdp_year3, aes(year, temp_rise)) + geom_point()

# Convert from wide to long dataframe for facet_wrap with free-y, separate y scales
gdp_year_long <- gather(gdp_year3, condition, measurement, totgdp, carbon, temp_rise)
# Label each facet appropriately
fac_label <- c("totgdp"="World GDP, $Trillion", "carbon"="Carbon Emissions, GTon", "temp_rise"= "Temperature Rise, F")
ggplot(gdp_year_long, aes (year, measurement, color=condition)) +  geom_point() +
  facet_wrap( ~ condition, ncol=1, scales = "free_y", labeller=as_labeller(fac_label))+
  theme(legend.position="none")+
    labs(title="GDP, Carbon Emission and Temp Rise are Correlated",
         x=NULL, y=NULL, caption="Source: Gapminder and temp_carbon Datasets") +
  theme(strip.background = element_rect(colour = NULL, fill = "white"),
        strip.text.x = element_text(colour = "black", face = "bold"))

Repeat the plots above but using more nicely formatted graphs

Use both points and lines connecting the points for clarity, except for Temperature Increase, where a linear fit is more appropriate. Other fits were not justifiable.

p1 <- ggplot(gdp_year3, aes (year, totgdp)) +
  geom_point(size = 1, alpha = 0.75, color="green") +
  geom_line(color="green") +
  theme_bw() +
  theme(axis.title.x = element_blank(), axis.title.y = element_text(size=10)) +
  labs(title="Global GDP Rises Annually", x=NULL, y="GDP, $Trillion")
p2 <- ggplot(gdp_year3, aes (year, carbon)) +
  geom_point(size = 1, alpha = 0.75, color="black") +
  geom_line(color="black") +
  theme_bw() +
  theme(axis.title.x = element_blank(), axis.title.y = element_text(size=10)) +
  labs(title="Carbon Emission Rises Proportionally", x=NULL, y="Carbon Emitted, GTon")
p3 <- ggplot(gdp_year3, aes (year, temp_rise)) +
  geom_point(size = 1, alpha = 0.75, color="red") +
  geom_smooth(method="lm",  color="black") +
  theme_bw() + 
  theme(axis.title.x = element_blank(), axis.title.y = element_text(size=10)) +
  labs(title="Global Temperature Increases As Well", x=NULL, y="Temp Increase, F",
       caption="Source: Gapminder and temp_carbon Datasets")

p1

p2

p3
## `geom_smooth()` using formula 'y ~ x'

Produce a 3-panel plot using a grid

The grid plot is very clear, competing with facet_wrap

# Grid plots require graphic objects  (grob)
p1<-as_grob(p1)   
p2<-as_grob(p2)
p3<-as_grob(p3)
## `geom_smooth()` using formula 'y ~ x'
# Exploratory work to create a composite plot, with GDP as main plot, and Carbon and Temperature as # insets within the main plot. It is harder to compare correlations; so I commented this out.
#grid.draw(p1)
#vp1= viewport (x=.35, y=.75, width=.45, height = .4)
#pushViewport(vp1)
#grid.draw(p2)
#upViewport()
#vp2= viewport (x=.70, y=.30, width=.45, height = .4)
#pushViewport(vp2)
#grid.draw(p3)
#upViewport()
#grid.newpage()

plot_grid (p1, p2, p3, ncol=1, align="v")