---
title: "ANLY 512 Lab 2"
author: "Qin Zhu"
date: "1/18/2020"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
source: embed
---
```{r}
## package installation
##devtools::install_github("ropensci/rnoaa")
##install.packages("plotly")
##install.packages("dygraphs")
library("plotly")
library("dygraphs")
library("rnoaa")
```
# Global Temperature
## Sidebar {.sidebar}
-----------------------------------------------------------------------
### Global Warming Situation
Global warning has been a serious concern to everyone in the past few decades. By viewing the graph on the left (Figure 1), temperature has an uprising trend since 1850 to. The positive slope from the graph suggests a continuous increase in temperature after 2000. Therefore, it is necessary to open the discussion regarding global warming to the public; and research the causes of global warning and potential opportunities to mitigate global warming risks. There are many related factors of global warming and CO2 will be discussed primary in this dashboard. As figure 2 shows, the amount of CO2 emission has been increasing in recent years, which is highly related to global warming effect.
## Column {.tabset}
-----------------------------------------------------------------------
### Figure 1. Global Temperature through the Years
```{r}
globaltemp <- read.csv(file="temperature-anomaly.csv")
globaltemp = subset(globaltemp, Entity == "Global")
globaltemp = globaltemp[,3:6]
p <- plot_ly(globaltemp, x = ~Year, y = ~Upper, type = 'scatter', mode = 'lines',
line = list(color = 'transparent'),
showlegend = FALSE, name = 'Upper') %>%
add_trace(y = ~Lower, type = 'scatter', mode = 'lines',
fill = 'tonexty', fillcolor='rgba(0,100,80,0.2)', line = list(color = 'transparent'),
showlegend = FALSE, name = 'Lower') %>%
add_trace(x = ~Year, y = ~Median, type = 'scatter', mode = 'lines',
line = list(color='rgb(0,100,80)'),
name = 'Median') %>%
layout(title = "Median, High and Low Temperatures in Global",
paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
xaxis = list(title = "Year",
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE),
yaxis = list(title = "Temperature",
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE))
p
```
### Figure 2. Global CO2 Emissions Through Year
```{r}
library(plotly)
CO2ByNation <- read.csv(file = "annual-co-emissions-by-region.csv")
CO2ByNation <- CO2ByNation[,-2]
CO2ByNation <- subset(CO2ByNation, Entity == "World")
plot_ly(y = ~CO2ByNation$Annual.emissions, x = ~CO2ByNation$Year, type = 'scatter', mode = 'lines', name = 'CO2 emissioins',fill = 'tozeroy') %>%
layout(xaxis = list(title = 'Year'),
yaxis = list(title = 'Global CO2 emissions In Total'))
```
# CO2 Emission By Source & Sector
## Column {.tabset}
-----------------------------------------------------------------------
### Figure 3. CO2 Emission By Source
```{r}
CO2BySource <- read.csv(file = "CO2-by-source.csv", col.names = c("Entity","Code","Year","CO2from gas (tonnes)","CO2from solid fuel (tonnes)","CO2from liquid (tonnes)","CO2from cement (tonnes)","CO2from flaring (tonnes)"))
CO2BySource = subset(CO2BySource, Entity=='World')
CO2BySource = CO2BySource[,3:8]
gas <- CO2BySource$CO2from.gas..tonnes.
solidfuel <- CO2BySource$CO2from.solid.fuel..tonnes.
liquid <- CO2BySource$CO2from.liquid..tonnes.
cement <- CO2BySource$CO2from.cement..tonnes.
flaring <- CO2BySource$CO2from.flaring..tonnes.
year <- CO2BySource$Year
plot_ly(y = ~gas, x = ~year, type = 'scatter', mode = 'lines', name = 'CO2 from Gas', fill = 'tozeroy') %>%
add_trace(y = ~solidfuel, x = ~year, name = 'CO2 from Solid Fuel', fill = 'tozeroy') %>%
add_trace(y = ~liquid, x = ~year, name = 'CO2 from Liquid', fill = 'tozeroy') %>%
add_trace(y = ~cement, x = ~year, name = 'CO2 from Cement', fill = 'tozeroy') %>%
add_trace(y = ~flaring, x = ~year, name = 'CO2 from Flaring', fill = 'tozeroy') %>%
layout(xaxis = list(title = 'Year'),
yaxis = list(title = 'Global CO2 emissions by source'),
hovermode = 'compare')
```
### Figure 4. CO2 Emission By Sector
```{r}
CO2bySector <- read.csv(file="carbon-dioxide-co2-emissions-by-sector-or-source.csv")
CO2bySector = subset(CO2bySector, Entity=='World')
CO2bySector = CO2bySector[,3:8]
Transport <- CO2bySector$Transport....
Electricity_and_Heat_Production <- CO2bySector$Electricity.and.heat.production....
Manufacturing_Industries_construction <- CO2bySector$Manufacturing.industries...construction....
Other_Factors <- CO2bySector$Other.sectors....
Residential_Buildings_commercial_public_Services <- CO2bySector$Residential.buildings...commercial.and.public.services....
year <- CO2bySector$Year
plot_ly(CO2bySector, y = ~Transport, x = ~year, type = 'scatter', mode = 'lines', name = 'CO2 from Transport', fill = 'tozeroy') %>%
add_trace(y = ~Electricity_and_Heat_Production, x = ~year, name = 'CO2 from Electricity and Heat Production', fill = 'tozeroy') %>%
add_trace(y = ~Residential_Buildings_commercial_public_Services, x = ~year, name = 'CO2 from Residential Buildings and Commercial Public Services', fill = 'tozeroy') %>%
add_trace(y = ~Other_Factors, x = ~year, name = 'CO2 from Others', fill = 'tozeroy') %>%
layout(xaxis = list(title = 'Year'),
yaxis = list(title = 'Global CO2 emissions by sector'),
hovermode = 'compare')
```
## Sidebar {.sidebar}
### CO2 Emission Source & Sector Analysis
From Figure 2, it's not hard to find that the amount of CO2 emission has increased in the last decades with a diversity of sources. Among those, solid fuel is still the top source of CO2 emission, followed by liquid and gas.
When it comes to CO2 emission sector, as figure 3 shows, electricity and head production has become the main sector of CO2 emissioin, while the amount of emssions from residential buildings and commercial public services has decreased over years.
# CO2 Emission in 2017
## Column {.tabset}
-----------------------------------------------------------------------
### Figure 5. Cumulative CO2 Emission By Nation in 2017
```{r}
library(dplyr)
library(ggplot2)
cumulative_emission <- read.csv(file="cumulative-co2-emissions-region.csv")
cumulative_emission<- subset(cumulative_emission, Entity != "World" & Year == "2017")
cumulative_emission <- cumulative_emission %>%
arrange(desc(Cumulative.CO..emissions..tonnes.)) %>%
slice(1:10)
cumulative_emission$emission <- cumulative_emission$Cumulative.CO..emissions..tonnes.
ggplot(cumulative_emission,aes(x = reorder(cumulative_emission$Entity, cumulative_emission$emission), y = cumulative_emission$emission))+ geom_bar(stat="identity",color = "#888888", aes(fill = cumulative_emission$emission)) + coord_flip() +
ggtitle("Top Ten Cumulative CO2 Emission By Nation in 2017") + theme_bw() + scale_fill_distiller (palette = "Reds", direction = 1)+
theme(panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
xlab("CO2 Emission")+
ylab("Nation")+
labs(fill = "CO2 Emission")
```
### Figure 6. Annual CO2 Emission By Nation in 2017
```{r}
emission <- read.csv(file="annual-co-emissions-by-region.csv")
emission<- subset(emission, Entity != "World" & Year == "2017")
emission <- emission %>%
arrange(desc(emission$Annual.emissions)) %>%
slice(1:10)
emission$CO2 <- cumulative_emission$Cumulative.CO..emissions..tonnes.
ggplot(emission,aes(x = reorder(emission$Entity, emission$CO2), y = emission$CO2))+ geom_bar(stat="identity",color = "#888888", aes(fill = CO2)) + coord_flip() +
ggtitle("Top Ten Annual CO2 Emission By Nation in 2017") + theme_bw() + scale_fill_distiller (palette = "Reds", direction = 1)+
theme(panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
xlab("CO2 Emission")+
ylab("Nation")+
labs(fill = "CO2 Emission")
```
## Sidebar {.sidebar}
### CO2 Emission By Region in 2017
There has been a lot of discussion of which nation(s) or region(s) should take more responsibility for global warming. And from figure 6, we can see that China is the nation with the most CO2 amount, estimated around 2.8 million CO2 emission, followed by the US, around 1.4 million CO2 emission. But if we look at the data from another dimension--cumulative CO2 emissions through 2017, United Staes is the top nation with the most CO2 emission amount, followed by EU and China.