wdi <- read.csv('WDI Data/WDIData.csv', header = TRUE, stringsAsFactors = FALSE)

wdi <- as.data.frame(wdi)
colnames(wdi)[1] <- 'Country.Name'
colnames(wdi)[5:length(colnames(wdi))] <- gsub(pattern = 'X', replacement = '', x = colnames(wdi)[5:length(colnames(wdi))])
indicator <- c('CO2 emissions from transport (% of total fuel combustion)', 
               'CO2 emissions from residential buildings and commercial and public services (% of total fuel combustion)',
               'CO2 emissions from manufacturing industries and construction (% of total fuel combustion)', 
               'CO2 emissions (metric tons per capita)',
               'CO2 emissions from electricity and heat production, total (% of total fuel combustion)',
               'CO2 emissions from gaseous fuel consumption (% of total)',
               'CO2 emissions from liquid fuel consumption (% of total)')
country <- c('United Sates', 'United Kingdom', 'Germany', 'China', 'Japan')

wdifiltered <- wdi %>% 
                select(1,3,45:61) %>%
                filter(Indicator.Name %in% indicator, Country.Name %in% country) %>%
                melt(id = c('Country.Name', 'Indicator.Name'))

CO2 emissions from transport (% of total fuel combustion)

CO2transport <- wdifiltered %>%
          filter(Indicator.Name == 'CO2 emissions from transport (% of total fuel combustion)')
min_x <- min(as.numeric(as.character(CO2transport$variable)))
max_x <- max(as.numeric(as.character(CO2transport$variable)))

X <- ggplot(CO2transport, aes(x = as.numeric(levels(variable))[variable], y = value)) + 
  geom_bar(stat="identity") +
  #geom_line() +
  labs(title = CO2transport$Indicator.Name[1], x = 'year', y = '%') +
  scale_x_continuous(breaks = round(seq(min_x, max_x, by = 1),0)) +
  facet_grid(Country.Name~.) +
  theme_gdocs()
ggplotly(X)
CO2residentialcommercial <- wdifiltered %>%
          filter(Indicator.Name == 'CO2 emissions from residential buildings and commercial and public services (% of total fuel combustion)')
min_x <- min(as.numeric(as.character(CO2residentialcommercial$variable)))
max_x <- max(as.numeric(as.character(CO2residentialcommercial$variable)))

Y <- ggplot(CO2residentialcommercial, aes(x = as.numeric(levels(variable))[variable], y = value)) + 
  geom_bar(stat="identity") +
  #geom_line() +
  labs(title = CO2residentialcommercial$Indicator.Name[1], x = 'year', y = '%') +
  scale_x_continuous(breaks = round(seq(min_x, max_x, by = 1),0)) +
  facet_grid(Country.Name~.) +
  theme_gdocs()
ggplotly(Y)
CO2emissions <- wdifiltered %>%
          filter(Indicator.Name == 'CO2 emissions (metric tons per capita)')
min_x <- min(as.numeric(as.character(CO2emissions$variable)))
max_x <- max(as.numeric(as.character(CO2emissions$variable)))

Z <- ggplot(CO2emissions, aes(x = as.numeric(levels(variable))[variable], y = value)) + 
  geom_bar(stat="identity") +
  #geom_line() +
  labs(title = CO2emissions$Indicator.Name[1], x = 'year', y = 'metric ton per capita') +
  scale_x_continuous(breaks = round(seq(min_x, max_x, by = 1),0)) +
  facet_grid(Country.Name~.) +
  theme_gdocs()
ggplotly(Z)
CO2electricity <- wdifiltered %>%
          filter(Indicator.Name == 'CO2 emissions from electricity and heat production, total (% of total fuel combustion)')
min_x <- min(as.numeric(as.character(CO2electricity$variable)))
max_x <- max(as.numeric(as.character(CO2electricity$variable)))

A <- ggplot(CO2electricity, aes(x = as.numeric(levels(variable))[variable], y = value)) + 
  geom_bar(stat="identity") +
  #geom_line() +
  labs(title = CO2electricity$Indicator.Name[1], x = 'year', y = '%') +
  scale_x_continuous(breaks = round(seq(min_x, max_x, by = 1),0)) +
  facet_grid(Country.Name~.) +
  theme_gdocs()
ggplotly(A)
CO2fuel <- wdifiltered %>%
          filter(Indicator.Name == 'CO2 emissions from gaseous fuel consumption (% of total)')
min_x <- min(as.numeric(as.character(CO2fuel$variable)))
max_x <- max(as.numeric(as.character(CO2fuel$variable)))

B <- ggplot(CO2fuel, aes(x = as.numeric(levels(variable))[variable], y = value)) + 
  geom_bar(stat="identity") +
  #geom_line() +
  labs(title = CO2fuel$Indicator.Name[1], x = 'year', y = '%') +
  scale_x_continuous(breaks = round(seq(min_x, max_x, by = 1),0)) +
  facet_grid(Country.Name~.) +
  theme_gdocs()
ggplotly(B)
CO2liquidfuel <- wdifiltered %>%
          filter(Indicator.Name == 'CO2 emissions from liquid fuel consumption (% of total)')
min_x <- min(as.numeric(as.character(CO2liquidfuel$variable)))
max_x <- max(as.numeric(as.character(CO2liquidfuel$variable)))

C <- ggplot(CO2liquidfuel, aes(x = as.numeric(levels(variable))[variable], y = value)) + 
  geom_bar(stat="identity") +
  #geom_line() +
  labs(title = CO2liquidfuel$Indicator.Name[1], x = 'year', y = '%') +
  scale_x_continuous(breaks = round(seq(min_x, max_x, by = 1),0)) +
  facet_grid(Country.Name~.) +
  theme_gdocs()
ggplotly(C)