Historical Temperature

Column

Global Temperature

Temperature by Seasons

Column

Temperature by Seasons

Global Temperature (Map)

Column

Global Temperature (Map)

Temperature Change by Major Cities

Column

### Temperature Change by Major Cities

---
title: "Climate Data Analysis"
Author: "Jiaoyuan Huang & Meng Zhang"
output:
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source_code: embed
---
  ```{r setup, include=FALSE}
library(ggplot2)
library(grid)
library(gridExtra)
library(RColorBrewer)
library(ggthemes)
library(dplyr)
library(sp)
library(rworldmap)
library(data.table)
library(Amelia)
library(caret)
library(ggfortify)
library(forecast)
library(imputeTS)
library(maps)

options(width=100)
knitr::opts_chunk$set(out.width='1000px',dpi=200,message=FALSE,warning=FALSE)

```

Historical Temperature {data-orientation=columns}
==========================================================================
  
  Sidebar {.sidebar}
-----------------------------------------------------------------------
  ### Climate Change
  
Q1: Is climate change real? 
Of course Yes! This dashboard will show you the fact.
Q2: How to prove it?
We will show the overall temperature started from 1850 to 2012. And we will show you some the different views about the temperature.

The data we used in this dashboard to show the climate change is from Berkley Earth (https://www.kaggle.com/berkeleyearth/climate-change-earth-surface-temperature-data).

We can see from this page that the overall increase is close to 2 degrees centigrade from 1850 to 2000. The Land Average temperature by year will show you that the temperature is steadily increasing. Also we will show the visualization of temperature distribution across the world.


Column 
-----------------------------------------------------------------------

### Global Temperature 
```{r}
glob<-read.csv("~/GlobalTemperatures.csv")
glob$date<-as.Date(glob$dt)
glob$year<-as.numeric(format(glob$date,'%Y'))
glob %>%
  select(LandAverageTemperature, LandAverageTemperatureUncertainty, year) %>%
  group_by(year) %>%
  filter(year>=1850) %>% 
  summarise(avgTemp = mean(LandAverageTemperature), avgError =mean(LandAverageTemperatureUncertainty)) %>% 
  ggplot(aes(x=year,y = avgTemp)) + 
  geom_point(aes(size=avgError,color=avgTemp),alpha=.75) + 
  theme(legend.position='top') + 
  theme_fivethirtyeight() + ylim(5,12) + 
  ggtitle('Global Temperature') + 
  geom_smooth(color='black',size=.4) + 
  labs(size = 'Average errors') + scale_color_gradientn(name='Degrees     Centigrade',colors=rev(brewer.pal(10,'Spectral'))) +
  scale_size(guide = 'none')
```

Temperature by Seasons {data-orientation=columns}
==========================================================================
  
  Sidebar {.sidebar}
-----------------------------------------------------------------------
  ### Temperature by Seasons
This page shows you the temperature from 1850 to 2012. We grouped the data into four seasons(Fall, Spring, Summer and Winter). The temperature of winer is slightly going up, because global warm. And it is very clear to see that the temperature of fall increased obviously. Summer and spring turn to be warmer too. This is one of the proof to show that climate change is real.


Column 
-----------------------------------------------------------------------

### Temperature by Seasons

```{r}
GT = fread("~/GlobalTemperatures.csv")
GT[,dt:=as.Date(GT[,dt])]
GT[,year:=year(GT[,dt])]
GT[,month:=month(GT[,dt])]
GT[,season:=ifelse(month %in% c(6,7,8),"Summer",
ifelse(month %in% c(9,10,11),"Fall",
ifelse(month %in% c(12,1,2),"Winter","Spring")
))]
ggplot(data = GT, 
aes(dt,LandAverageTemperature, colour=season)) +
geom_point(na.rm = T) +
xlab("Year") +
ggtitle("Temperature by Seasons")

```


Global Temperature (Map) {data-orientation=columns}
==========================================================================
  
  Sidebar {.sidebar}
-----------------------------------------------------------------------
  ### Global Temperature (Map)
  
This is a world map to show how the temperature change happens all over the world. We can see from the map that even for the very north area, still have relatively high temperature than it is supposed to be.


Column
-----------------------------------------------------------------------

### Global Temperature (Map)

```{r}
convert<-function(x){
westEast<-substr(x,nchar(x),nchar(x))
xx<-substr(x,1,nchar(x)-1)
val<-as.numeric(char2dms(paste0(strsplit(xx,'[.]')[[1]][1],'d',strsplit(xx,'[.]')[[1]][1],"'",westEast)))
return(val)
}
worldMap <- fortify(map_data("world"), region = "region")
m <- ggplot() +
geom_map(data = worldMap, map = worldMap,aes(x = long, y = lat, map_id = region, group = group),fill = "white", color = "black", size = 0.1)

allCities<-read.csv('~/GlobalLandTemperaturesByCity.csv')
allCities<-na.omit(allCities)
allCities$date<-as.Date(allCities$dt)
allCities$year<-as.numeric(format(allCities$date,'%Y'))
allCities <-as.data.frame(allCities %>% filter(year>=1850))
allCities$month<-as.numeric(format(allCities$date,'%m'))
allCities$Longitude<-as.character(allCities$Longitude)
allCities$Latitude<-as.character(allCities$Latitude)

start<-allCities %>% filter(year==1850)
start$LONG<-sapply(start$Longitude,convert)
start$LAT<-sapply(start$Latitude,convert)
start<-as.data.frame(start %>% group_by(Country, City) %>% select(AverageTemperature, City ,LAT, LONG, Country) %>% summarise(avgTemp_start = mean(AverageTemperature), long = mean(LONG), lat = mean(LAT)))

end<-as.data.frame(allCities %>% filter(year==2012))
end<-as.data.frame(end %>% group_by(Country,City) %>% select(AverageTemperature,City) %>% summarise(avgTemp_end = mean(AverageTemperature)))

res2<-as.data.frame(merge(start,end, by=c('Country','City')))

m +
geom_point(data=res2,aes(x=long, y=lat, size=avgTemp_end - avgTemp_start,color=avgTemp_end - avgTemp_start),alpha=.2) +
theme_fivethirtyeight() +
ggtitle('Global Temperature Map') +
theme(axis.text = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + labs(size = '') +
scale_color_gradientn(name='Degrees Celsius',colors=rev(brewer.pal(10,'Spectral'))) + scale_size(guide = 'none')

```

Temperature Change by Major Cities {data-orientation=columns}
==========================================================================
  
  Sidebar {.sidebar}
-----------------------------------------------------------------------
  
  ### Temperature Change by Major Cities
  
  The visualization shown here enumerates the worst affected cities in the world in terms of temperature. In addition to the temperature rise consequences, flooding is another major disastrous consequence. This is not shown in the visualization, however, some of the smaller islands around the world are already going under the sea, causing major population migration to the central parts of the land and in turn causing overcrowding.  



Column 
-----------------------------------------------------------------------
  
  ### Temperature Change by Major Cities
  
  ```{r}

convert<-function(x){
  westEast<-substr(x,nchar(x),nchar(x))
  xx<-substr(x,1,nchar(x)-1)
  val<-as.numeric(char2dms(paste0(strsplit(xx,'[.]')[[1]][1],'d',strsplit(xx,'[.]')[[1]][1],"'",westEast)))
  return(val)
}

majorCities<-read.csv('~/GlobalLandTemperaturesByMajorCity.csv')
majorCities<-na.omit(majorCities)
majorCities$date<-as.Date(majorCities$dt)
majorCities$year<-as.numeric(format(majorCities$date,'%Y'))
majorCities <-as.data.frame(majorCities %>% filter(year>=1850))
majorCities$month<-as.numeric(format(majorCities$date,'%m'))
majorCities$Longitude<-as.character(majorCities$Longitude)
majorCities$Latitude<-as.character(majorCities$Latitude)

start<-majorCities %>% filter(year==1850)
start$LONG<-sapply(start$Longitude,convert) 
start$LAT<-sapply(start$Latitude,convert)

start<-as.data.frame(start %>% group_by(Country, City) %>% select(AverageTemperature, City ,LAT, LONG, Country) %>% summarise(avgTemp_start = mean(AverageTemperature), long = mean(LONG), lat = mean(LAT)))

end<-as.data.frame(majorCities %>% filter(year==2012))
end<-as.data.frame(end %>% group_by(Country,City) %>% select(AverageTemperature,City) %>% summarise(avgTemp_end = mean(AverageTemperature)))

res2<-as.data.frame(merge(start,end, by=c('Country','City')))

ggplot(data=res2,aes(x=reorder(City,avgTemp_end - avgTemp_start),y=avgTemp_end - avgTemp_start)) + 
  geom_histogram(aes(fill=avgTemp_end - avgTemp_start),stat='identity',width=.75) + 
  coord_flip() + 
  theme_fivethirtyeight() + 
  ggtitle('World Major Cities') + 
  scale_fill_gradientn(name='Temperature Change by Major Cities',colours=rev(brewer.pal(10,'Spectral'))) +
  theme(legend.position='top',axis.text = element_text(size=7))
```