Hail Event Across US

column

Colorpleth 2016

Colorpleth 2017

Hail Event Counts by State (Bar Chart)

row

Hail Event Bar Chart 2016

Texas Hail Events

column

Texas Hail Events

1280x1280 terrain map image from Google Maps.  see ?ggmap to plot it.

Column

Monthly Hail Event Counts in Texas in 2016

Monthly Hail Event Counts in Texas in 2017

---
title: "Lab 2 -- Hail Event"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
---

Hail Event Across US
=====================================

```{r setup, include=FALSE}
#install.packages("scales")
#install.packages("RColorBrewer")
#install.packages("ggplot2")
#install.packages("fiftystater")
#install.packages("plyr")
#install.packages("noncensus")
#install.packages("ggmap")
#install.packages("ggrepel")
library(scales)
library(RColorBrewer)
library(readxl)
library(ggplot2)
library(fiftystater)
library(plyr)
library(noncensus)
library(ggmap)
library(ggrepel)
library(flexdashboard)

```

Intro {.sidebar}
------------------------------------------
Q1: What's the pattern of Hail Events in United States? Which state has the highest hail event counts in 2016 and 2017? 

* In 2016, Texas has the highest hail event counts in the US and states in the west and east coast have much lower hail event counts than other sates. In 2017, Texas still has the highest hail event counts and the hail event counts for states in the southeast is much higher compared with the counts in 2016.

column
--------------------------------------
###Colorpleth 2016
```{r}
Severe <- read.csv("D:/HU/2018 Summer/ANLY512/Lab/Lab2/Data/Storm Event/StormEvents_details-ftp_v1.0_d2017_c20180718.csv")
Severe16 <- read.csv("D:/HU/2018 Summer/ANLY512/Lab/Lab2/Data/Storm Event/StormEvents_details-ftp_v1.0_d2016_c20180718.csv")

Hail16 <- subset(Severe16, EVENT_TYPE == "Hail" )

Hail16.count <- count(Hail16, 'STATE')
Hail16.count <- rbind(Hail16.count, "50" = c("ALASKA", "0"))
Hail16.count <- rbind(Hail16.count, "51" = c("HAWAII", "0"))
Hail16.count <- rbind(Hail16.count, "52" = c("CONNECTICUT", "0"))
Hail16.count$freq <- as.numeric(Hail16.count$freq)#define as numeric in order to use scale in ggplot
Hail16.count$statelower <- tolower(Hail16.count$STATE)#lowercase state column in order to combine the name for fifty states
Hail16.count$stateabb <- c("AL","AZ","AR","CA","CO","DE","DC","FL","GA","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA",
                         "MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","PR","RI",
                         "SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY","AK","HI","CT" )


data("fifty_states")#get the states data from the package

visual_1_16 <- ggplot(Hail16.count, aes(map_id = statelower)) +
  geom_map(aes(fill = freq),color = "grey", map = fifty_states) +
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "", title="Counts of Hail Events by State in 2016") +
  scale_fill_distiller(name="Count",palette= "Spectral", breaks = pretty_breaks(n=7)) + 
  theme(panel.background = element_blank(), plot.title = element_text(hjust=0.5))

visual_1_16
```

###Colorpleth 2017

```{r}

Hail <- subset(Severe, EVENT_TYPE == "Hail" )
#Hail event count by state, prepare the dataset for 
Hail.count <- count(Hail, 'STATE')
Hail.count <- rbind(Hail.count, "51" = c("ALASKA", "0"))
Hail.count <- rbind(Hail.count, "52" = c("HAWAII", "0"))
Hail.count$freq <- as.numeric(Hail.count$freq)#define as numeric in order to use scale in ggplot
Hail.count$statelower <- tolower(Hail.count$STATE)#lowercase state column in order to combine the name for fifty states
Hail.count$stateabb <- c("AL","AZ","AR","CA","CO","CT","DE","DC","FL","GA","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA",
                        "MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","PR","RI",
                        "SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY","AK","HI" )


data("fifty_states")#get the states data from the package

visual_1_17 <- ggplot(Hail.count, aes(map_id = statelower)) +
  geom_map(aes(fill = freq),color = "grey", map = fifty_states) +
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "", title="Counts of Hail Events by State in 2017") +
  scale_fill_distiller(name="Count",palette= "Spectral", breaks = pretty_breaks(n=7)) + 
  theme(panel.background = element_blank(), plot.title = element_text(hjust=0.5))

visual_1_17

```

Hail Event Counts by State (Bar Chart)
=========================================
Intro {.sidebar}
-------------------------------------------
Q2: Any significant change in hail event counts for each state from 2016 to 2017?

* Iowa, Illinois, Mossouri, and Wiscosin has much more hail events happened in 2017 than that in 2016.

row
------------------------------------------------
### Hail Event Bar Chart 2016

```{r,fig.width=10.5,fig.height=6}
Hail16.count$Year <- rep("2016",52)
Hail.count$Year <- rep("2017",52)
Hail.combine <- rbind(Hail16.count,Hail.count)

ggplot(data = Hail.combine, aes(x=stateabb,y = freq, fill = Year))+
geom_bar(stat="identity", position = position_dodge())+
  xlab("State") + ylab("Count of Hail Event") +
  theme(legend.position = "bottom", plot.title = element_text(hjust=0.5) ) +
  ggtitle("Number of Hail Count in the United States by State Comparison 2016 - 2017") +
  geom_text(aes(label = freq), color="black", vjust=-0.5, position = position_dodge(0.9),size = 2) +
  scale_fill_manual(values=c('#FFCC99','#FF6666'))

```

Texas Hail Events
===============================
Intro {.sidebar}
------------------------------------------
Since Texas has the highest hail counts in the US, we would like to know more about the hail pattern in Texas.

Q3: In the state of Texas, what's the pattern of hail events? Any area has the highest hail counts?

* According to the map on the top, it seems like hail happened evenly across all area in Texas.

Q4: When the hail event most likely happen during a year? Any change from 2016 to 2017?

* In 2016, hail happened mostly in May, April, and March. In 2017, hail happened mostly in May, April, March and June. And compared with 2016, the hail event counts increased a lot for month of June in 2017.

column
--------------------------------------------------------------
###Texas Hail Events

```{r}
#Combine 16 and 17 dataset
Hail16.texas <- subset(Hail16, STATE == "TEXAS")
Hail16.texas <- Hail16.texas[c("BEGIN_LON","BEGIN_LAT")]
Hail16.texas$Year <- rep("2016",1388)

Hail.texas <- subset(Hail, STATE == "TEXAS")
Hail.texas <- Hail.texas[c("BEGIN_LON","BEGIN_LAT")]
Hail.texas$Year <- rep("2017",1290)

texas <- rbind(Hail16.texas,Hail.texas)

#Visualization
TX <- get_map(location = "Texas", source = "google", crop = FALSE, zoom = 6)
TX
 ggmap(TX) +
    xlab("Longtitude") + ylab("Latitude") +
    theme(legend.position = "bottom", plot.title = element_text(hjust=0.5))+
    geom_point(data = texas, aes(x = BEGIN_LON, y = BEGIN_LAT, size = 8, color = factor(Year))) +
    ggtitle("Hail Event in Texas (Year 2016 - 2017)")

```

Column {.tabset .tabset-fade}
-----------------------------------------------------------------------

### Monthly Hail Event Counts in Texas in 2016

```{r}

#---------- 2016 ------------#
#get frequncy table for month 
Hail16.texas <- subset(Hail16, STATE == "TEXAS" & !MONTH_NAME %in% c("November","December"))
Hail.texas.c16 <- count(Hail16.texas,'MONTH_NAME')
Hail.texas.c16u <- Hail.texas.c16[order(-Hail.texas.c16$freq),]
pct16 <- round(Hail.texas.c16u$freq/sum(Hail.texas.c16u$freq)*100)
lbls16 <- paste(Hail.texas.c16u$MONTH_NAME,pct16)
lbls16 <- paste(lbls16,"%",sep="")
pie(Hail.texas.c16u$freq,labels = lbls16, size =0.5, col = c("#9E0142","#D53E4F","#F46D43","#FDAE61","#FEE08B","#FFFFBF","#E6F598","#ABDDA4","#66C2A5","#3288BD","#5E4FA2","grey"), main = "Hail Count in Texas by Month (Year 2016)")

```

### Monthly Hail Event Counts in Texas in 2017

```{r}

#---------- 2017 ------------#
#get frequncy table for month 
Hail.texas <- subset(Hail, STATE == "TEXAS" & MONTH_NAME != "November")
Hail.texas.c17 <- count(Hail.texas,'MONTH_NAME')
Hail.texas.c17u <- Hail.texas.c17[order(-Hail.texas.c17$freq),]
pct17 <- round(Hail.texas.c17u$freq/sum(Hail.texas.c17u$freq)*100)
lbls17 <- paste(Hail.texas.c17u$MONTH_NAME,pct17)
lbls17 <- paste(lbls17,"%",sep="")
pie(Hail.texas.c17u$freq,labels = lbls17, size =1, col = c("#9E0142","#D53E4F","#F46D43","#FDAE61","#FEE08B","#FFFFBF","#E6F598","#ABDDA4","#66C2A5","#3288BD","#5E4FA2","grey"), main = "Hail Count in Texas by Month (Year 2017)")


```