# install.packages("tidyverse")
# install.packages("zoo")
library(tidyverse)
## ── Attaching packages ──────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.1 ✓ purrr 0.3.4
## ✓ tibble 3.0.1 ✓ dplyr 1.0.0
## ✓ tidyr 1.1.0 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ─────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(dslabs)
data("greenhouse_gases")
head(greenhouse_gases)
## year gas concentration
## 1 20 CO2 277.7
## 2 40 CO2 277.8
## 3 60 CO2 277.3
## 4 80 CO2 277.3
## 5 100 CO2 277.5
## 6 120 CO2 277.6
#Get info on the data set.
summary(greenhouse_gases)
## year gas concentration
## Min. : 20 Length:300 Min. : 260.0
## 1st Qu.: 515 Class :character 1st Qu.: 269.7
## Median :1010 Mode :character Median : 279.7
## Mean :1010 Mean : 416.2
## 3rd Qu.:1505 3rd Qu.: 641.0
## Max. :2000 Max. :1703.4
str(greenhouse_gases)
## 'data.frame': 300 obs. of 3 variables:
## $ year : num 20 40 60 80 100 120 140 160 180 200 ...
## $ gas : chr "CO2" "CO2" "CO2" "CO2" ...
## $ concentration: num 278 278 277 277 278 ...
# Add Title
ggplot(greenhouse_gases, aes(x = year, y = concentration, color = gas)) + geom_line()+ geom_point() + ggtitle("Greenhouse Gases since 1740")
# Add X and Y labels
ggplot(greenhouse_gases, aes(x = year, y = concentration, color = gas)) + geom_line() + geom_point() + ggtitle("Greenhouse Gases since 1740") + xlab("Year") + ylab("Concentration")
# Center title
# Change font to Times New Roman and Adjust Size
ggplot(greenhouse_gases, aes(x = year, y = concentration, color = gas)) + geom_line() + geom_point() + ggtitle("Greenhouse Gases since 1740") + xlab("Year") + ylab("Concentration") + theme(plot.title = element_text(hjust = 0.5), text = element_text(family = "Times New Roman", size= 14))
#Specify years of data collection
ggplot(greenhouse_gases, aes(x = year, y = concentration, color = gas)) + geom_line() + geom_point() + ggtitle("Greenhouse Gases since 1740") + xlab("Year") + ylab("Concentration") + theme(plot.title = element_text(hjust = 0.5), text = element_text(family = "Times New Roman", size= 14)) + xlim(min=1740, max = 2020)
## Warning: Removed 258 row(s) containing missing values (geom_path).
## Warning: Removed 258 rows containing missing values (geom_point).
#Change legend to spell out gas names completely
ggplot(greenhouse_gases, aes(x = year, y = concentration, color = gas)) + geom_line() + geom_point() + ggtitle("Greenhouse Gases since 1740") + xlab("Year") + ylab("Concentration") + theme(plot.title = element_text(hjust = 0.5), text = element_text(family = "Times New Roman", size= 14)) + xlim(min=1740, max = 2020) + labs(color = "Gases") + scale_color_hue(labels= c("CH4" = "Methane (CH4)", "CO2" = "Carbon Dioxide (CO2)", "N2O" = "Nitrous Oxide (N2O"))
## Warning: Removed 258 row(s) containing missing values (geom_path).
## Warning: Removed 258 rows containing missing values (geom_point).
# Adjust legend font size to allow more space for actual plot
ggplot(greenhouse_gases, aes(x = year, y = concentration, color = gas)) + geom_line() + geom_point() + ggtitle("Greenhouse Gases since 1740") + xlab("Year") + ylab("Concentration") + theme(plot.title = element_text(hjust = 0.5), text = element_text(family = "Times New Roman", size= 14)) + xlim(min=1740, max = 2020) + labs(color = "Gases") + scale_color_hue(labels= c("CH4" = "Methane (CH4)", "CO2" = "Carbon Dioxide (CO2)", "N2O" = "Nitrous Oxide (N2O")) + theme(legend.title = element_text(size = 8),
legend.text = element_text(size = 8))
## Warning: Removed 258 row(s) containing missing values (geom_path).
## Warning: Removed 258 rows containing missing values (geom_point).
#Add more tick marks to the x axis
ggplot(greenhouse_gases, aes(x = year, y = concentration, color = gas)) + geom_line() + geom_point() + ggtitle("Greenhouse Gases since 1740") + xlab("Year") + ylab("Concentration") + theme(plot.title = element_text(hjust = 0.5), text = element_text(family = "Times New Roman", size= 14)) + xlim(min=1740, max = 2020) + labs(color = "Gases") + scale_color_hue(labels= c("CH4" = "Methane (CH4)", "CO2" = "Carbon Dioxide (CO2)", "N2O" = "Nitrous Oxide (N2O")) + theme(legend.title = element_text(size = 8),
legend.text = element_text(size = 8)) + scale_x_continuous(n.breaks = 4)
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.
#Adjust axes font size
#Separate lines to make code more readable
#Make title text bold
#Remove tick marks in order to maintain previously designated scale of graph
ggplot(greenhouse_gases, aes(x = year, y = concentration, color = gas)) +
geom_line() + geom_point() +
ggtitle("Greenhouse Gases since 1740") +
xlab("Year") +
ylab("Concentration") +
theme(plot.title = element_text(hjust = 0.5), text = element_text(family = "Times New Roman", size= 14)) +
xlim(min=1740, max = 2020) + labs(color = "Gases") + scale_color_hue(labels= c("CH4" = "Methane (CH4)", "CO2" = "Carbon Dioxide (CO2)", "N2O" = "Nitrous Oxide (N2O")) +
theme(legend.title = element_text(size = 8), legend.text = element_text(size = 8)) + theme(axis.text=element_text(size=10),
axis.title=element_text(size=12, face = "bold")) +
theme(plot.title = element_text(face = "bold"))
## Warning: Removed 258 row(s) containing missing values (geom_path).
## Warning: Removed 258 rows containing missing values (geom_point).
Depicted here are the concentrations of the three main greenhouse gasses, from the year 1740 until the year 2000. The industrial revolution, which occurred during the second half of the 18th century, had a profound effect on the concentrations of greenhouse gases in our atmosphere. This is primarily due to the increased combustion of fossil fuels for transportation heat, and electricity. Although the burning of fossil fuels for our energy needs has made society much more efficient and capable, it has also done irreversible damage to our atmosphere. Greenhouse gases trap heat and accelerate climate change, which in turn causes extreme weather conditions such as wildfires, more intense hurricanes, and severe droughts.
I specifically chose the year 1740 as the starting point for the visual in order for the viewer to see a few data points in the years before the industrial revolution; these data points serve as a backdrop against which the spike in greenhouse gases can be seen. The data collection for this specific data set appears to have ceased in 2000; if the past 20 years were included as well, the increase in greenhouse gases would be even more dramatic, given that human beings continuously produce more waste and require more energy for various processes. Graphs like these make it abundantly clear that we, as citizens of Earth, must intensify our efforts to use more renewable energy practices in order to reduce the emission of greenhouse gases.