Graph 1. Carbon Dioxide Emissions (kt) across Years


Graph 1 shows the average carbon dioxide emissions (kt) in the world from the year of 1960 to 2011. Overall, the average amount of carbon dioxide emissions among countries increases throughout the years, and reaches its peak at 2011.

Graph 2. Carbon Dioxide Emissions by Country across Years


Graph 2 shows the Percentile rank of carbon dioxide emissions by country across years. The Top 20 countries were included in the graph. Overall, United States is observed to have the largest amount of carbon dioxide emission across years, followed by China. The emission in China kept increasing from 1960s to 2000s.

Graph 3. Greenhouse Gas Emissions across Years


Graph 3 reveals the trend of greenhouse gas emissions across years, including HFC, PFC, and SF6. In general, greenhouse gas emissions increased from the year of 1990 to 2005, arriving at the peak at 2005, and then decreased from 2005 to 2010. The amount of gas emission is highest for HFC, followed by PFC, and then SF6.

Graph 4. Percentage of Emission from Fuel Consumption across Years


Graph 4 shows the distribution of emission from fuel consumption across years. The percentage of solid fuel kept stable across years. However, the percentage of liquid fuel decreased from the year of 1960 to 2011, while the percentage of gaseous fuel increased across years.

---
title: "CO2"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    storyboard: true
    source_code: embed
---

```{r data}
library(flexdashboard)
library(ggplot2)
library(plyr)
library(plotly)
library(dplyr)
setwd("C:/Users/Hannah/Desktop/Assignment/ANLY512/WorldProject")
data<-read.csv("Indicators.csv", sep=",", header=T)
```


### Graph 1. Carbon Dioxide Emissions (kt) across Years

```{r Graph 1}
# Graph 1 CO2 emissions
data1 <- data[ which(data$IndicatorName=="CO2 emissions (kt)"),]

data1$Years[data1$Year >=1960 & data1$Year<1970] <- 1960
data1$Years[data1$Year >=1970 & data1$Year<1980] <- 1970
data1$Years[data1$Year >=1980 & data1$Year<1990] <- 1980
data1$Years[data1$Year >=1990 & data1$Year<2000] <- 1990
data1$Years[data1$Year >=2000 & data1$Year<2010] <- 2000

data1_emi<-as.data.frame(
  data1 %>% 
    group_by(Year) %>%                          
    summarise(mean_co2 = mean(Value)))

# Line Graph
g1<-ggplot(data1_emi, aes(x=Year, y=mean_co2, group=1)) +
  geom_line(color="blue")+
  geom_point(color="blue")+
  ggtitle("Graph 1. Carbon Dioxide Emissions (kt) across Years") +
  scale_x_continuous(breaks=seq(1960,2015,5))+
  xlab("Years") + ylab("Emissions (kt)")+
  theme_light()+
  theme(plot.title = element_text(hjust = 0.5))
ggplotly(g1)
```

*** 
Graph 1 shows the average carbon dioxide emissions (kt) in the world from the year of 1960 to 2011. Overall, the average amount of carbon dioxide emissions among countries increases throughout the years, and reaches its peak at 2011.

### Graph 2. Carbon Dioxide Emissions by Country across Years

```{r Graph 2}
# Graph 2 Heatmap for CO2 emission by country
library(scales)
library(reshape2)

data1_4<-as.data.frame(
  data1 %>% 
    group_by(CountryName, Years) %>%                          
    summarise(mean_co2 = mean(Value)))

# Unmelt data
data1_d<-dcast(data1_4, CountryName ~ Years)
data1_d$"NA"<-NULL
data1_d <- na.omit(data1_d)
data1_d$sum=data1_d$"1960"+data1_d$"1970"+data1_d$"1980"+data1_d$"1990"+data1_d$"2000"
data1_d <- data1_d[order(-data1_d$sum),] 

# Top 20 countries
data1_top<-data1_d[c(8, 13, 17,21,24,26,29,30,31,33,34,35,36,37,38,39,41,42,43,44), ]
data1_top$sum<-NULL

# Melt data
data1_m <- melt(data1_top, id=c("CountryName"))
data1_re <- ddply(data1_m, .(variable), transform, rescale = rescale(value))

# Heatmap
g2<-ggplot(data1_re, aes(variable, CountryName)) + 
  geom_tile(aes(fill = data1_re$rescale), colour = "white") + 
  scale_fill_gradient(low = "white", high = "steelblue", breaks=c(0,0.25, 0.5,0.75, 1),
                      name="Percentile Rank", labels=c("0th", "25th", "50th", "75th", "100th"))+
  ggtitle("Graph 2. Carbon Dioxide Emissions by Country across Years") +
  theme(plot.title = element_text(hjust = 0.5))+
  scale_x_discrete(breaks=c(1960, 1970, 1980, 1990, 2000), labels=c("1960s", "1970s","1980s", "1990s", "2000s"))+
  xlab("Years") + ylab("Country Name")
ggplotly(g2)

```

*** 
Graph 2 shows the Percentile rank of carbon dioxide emissions by country across years. The Top 20 countries were included in the graph. Overall, United States is observed to have the largest amount of carbon dioxide emission across years, followed by China. The emission in China kept increasing from 1960s to 2000s.


### Graph 3. Greenhouse Gas Emissions across Years

```{r Graph 3}
# Graph 3 greenhouse emissions 
data2 <- data[ which(data$IndicatorName=="HFC gas emissions (thousand metric tons of CO2 equivalent)"
                   | data$IndicatorName=="PFC gas emissions (thousand metric tons of CO2 equivalent)"
                   | data$IndicatorName=="SF6 gas emissions (thousand metric tons of CO2 equivalent)"),]

data2_green<-as.data.frame(
  data2 %>% 
    group_by(IndicatorName, Year) %>%                          
    summarise(mean_green = mean(Value)))

data2_green$Gas[data2_green$IndicatorName =="HFC gas emissions (thousand metric tons of CO2 equivalent)"] <- "HFC"
data2_green$Gas[data2_green$IndicatorName =="PFC gas emissions (thousand metric tons of CO2 equivalent)"] <- "PFC"
data2_green$Gas[data2_green$IndicatorName =="SF6 gas emissions (thousand metric tons of CO2 equivalent)"] <- "SF6"

# Line graph
g3<-ggplot(data2_green, aes(x=Year, y=mean_green, group=Gas)) +
  geom_line(aes(color=Gas))+
  geom_point(aes(color=Gas))+
  ggtitle("Graph 3. Greenhouse Gas Emissions across Years") +
  xlab("Years") + ylab("Emissions (Thousand Metric Tons)")+
  theme_light()+
  theme(legend.position="right")+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_colour_manual(name="Gas", values=c("red", "green", "blue"),labels = c("HFC", "PFC", "SF6"))
ggplotly(g3)
```

*** 
Graph 3 reveals the trend of greenhouse gas emissions across years, including HFC, PFC, and SF6. In general, greenhouse gas emissions increased from the year of 1990 to 2005, arriving at the peak at 2005, and then decreased from 2005 to 2010. The amount of gas emission is highest for HFC, followed by PFC, and then SF6.  


### Graph 4. Percentage of Emission from Fuel Consumption across Years

```{r Graph 4}
# Graph 4 Percentage of emission
data3_1 <- data[ which(data$IndicatorName=="CO2 emissions from gaseous fuel consumption (% of total)"),]
data3_1[,c("CountryName","IndicatorCode")] <- list(NULL)

data3_2 <- data[ which(data$IndicatorName=="CO2 emissions from liquid fuel consumption (% of total)"),]
data3_2[,c("CountryName","IndicatorCode")] <- list(NULL)

data3_3 <- data[ which(data$IndicatorName=="CO2 emissions from solid fuel consumption (% of total)"),]
data3_3[,c("CountryName","IndicatorCode")] <- list(NULL)

data3_12 <- merge(data3_1, data3_2, by=c("CountryCode","Year"))
data3_12[,c("IndicatorName.x","IndicatorName.y")] <- list(NULL)
colnames(data3_12)<-c("CountryCode","Year","gaseous","liquid")

data3 <- merge(data3_12, data3_3, by=c("CountryCode","Year"))
data3[,c("IndicatorName")] <- list(NULL)
colnames(data3)<-c("CountryCode","Year","gaseous","liquid","solid")
data3$other<-100-(data3$gaseous + data3$liquid + data3$solid)

data3_percent<-as.data.frame(
  data3 %>% 
    group_by(Year) %>%                          
    summarise(mean_g = mean(gaseous),
              mean_l = mean(liquid),
              mean_s = mean(solid),
              mean_o = mean(other)))

colnames(data3_percent)<-c("Year","Gaseous","Liquid","Solid", "Others")

mdata3 <- melt(data3_percent, id=c("Year"))

g4<-ggplot(mdata3, aes(Year, value, fill = variable))+ 
  geom_bar(stat="identity")+
  scale_y_continuous(limits = c(0, 100),labels = function(x) paste0(abs(x), "%"))+
  labs(x="Years", y="Percentage")+
  ggtitle("Graph 4. Percentage of Emission from Fuel Consumption across Years")+
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5))+
  scale_x_continuous(breaks=seq(1960,2015,5))+
  scale_fill_manual(name="Fuel", values=c("#a6cee3", "#1f78b4", "#b2df8a","#33a02c"))
ggplotly(g4)
```

*** 
Graph 4 shows the distribution of emission from fuel consumption across years. The percentage of solid fuel kept stable across years. However, the percentage of liquid fuel decreased from the year of 1960 to 2011, while the percentage of gaseous fuel increased across years.