if (!require("ggplot2")) { install.packages("ggplot2"); library(ggplot2) }
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.3.2
if (!require("dplyr")) { install.packages("dplyr"); library(dplyr) }
## Loading required package: dplyr
## Warning: package 'dplyr' was built under R version 3.3.2
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
if (!file.exists("storm")){
dir.create("storm")
}
fileurl<-"https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(fileurl,destfile = "storm.csv",method = "auto")
storm1<-read.table("storm.csv",header = TRUE,sep = ",")
stormfatal<-aggregate(storm1$FATALITIES, by=list(storm1$EVTYPE),sum, na.rm=TRUE)
fatal<-rep(c("FATALITIES"),times=985)
stormfatal<-cbind(stormfatal,fatal)
colnames(stormfatal)<-c("EVTYPE","CASES","HEALTH")
stormfatalMAX<-stormfatal[order(stormfatal$CASES,decreasing=TRUE)[1:4],]
storminj<-aggregate(storm1$INJURIES, by=list(storm1$EVTYPE),sum,na.rm=TRUE )
inju<-rep(c("INJURIES"),times=985)
storminj<-cbind(storminj,inju)
colnames(storminj)<-c("EVTYPE","CASES","HEALTH")
storminjMAX<-storminj[order(storminj$CASES,decreasing=TRUE)[1:4],]
health<-rbind(stormfatalMAX,storminjMAX)
levels(health$EVTYPE) <- tolower(levels(health$EVTYPE))
## We will use agreggate so we can sum all the losses per event type.
propdmg<-aggregate(storm1$PROPDMG, by=list(storm1$EVTYPE),sum, na.rm=TRUE)
#this rep it will be used to create 985 character vector named "Properties" to be used later for panel plot
property<-rep(c("Properties"),times=985)
propdmg<-cbind(propdmg,property)
colnames(propdmg)<-c("EVTYPE","CASES","DAMAGE")
#this step will define the most 4th influential events
propdmgMAX<-propdmg[order(propdmg$CASES,decreasing=TRUE)[1:4],]
cropdmg<-aggregate(storm1$CROPDMG, by=list(storm1$EVTYPE),sum,na.rm=TRUE )
crop<-rep(c("Crops"),times=985)
cropdmg<-cbind(cropdmg,crop)
colnames(cropdmg)<-c("EVTYPE","CASES","DAMAGE")
cropdmgMAX<-cropdmg[order(cropdmg$CASES,decreasing=TRUE)[1:4],]
econom<-rbind(propdmgMAX,cropdmgMAX)
levels(econom$EVTYPE) <- tolower(levels(econom$EVTYPE))
econom$CASES<-round(econom$CASES,digits = 0)
h<-ggplot(health, aes(x = EVTYPE, y = CASES,label=CASES)) +
theme_grey()+
geom_bar(aes(fill=EVTYPE),stat = "identity")+ facet_grid(HEALTH~., scales="free")+
geom_text(colour = "black", fontface = "italic", size= 4,hjust= 0.5,vjust=-0.25,angle=0)+
theme(axis.text.x=element_text(angle=90, size=10, vjust=0.5))+
theme(legend.title = element_blank())+
theme(axis.ticks.y = element_blank(),axis.text.y = element_blank())+
xlab("Event Types")+
ylab("Count (units)")+
ggtitle("The types of events most harmful to population health in USA")+
theme(plot.title = element_text(size = 15,lineheight = 0.3,vjust=0.5))+
coord_cartesian(ylim = c(0,95000))
print(h)
g<-ggplot(econom, aes(x = EVTYPE, y = CASES,label=round(CASES,0))) +
theme_grey()+
geom_bar(aes(fill=EVTYPE),stat = "identity")+ facet_grid(DAMAGE~.,scales = "free")+
geom_text(colour = "black", fontface = "italic", size= 4,hjust=0.5,vjust=-0.25,angle=0)+
theme(axis.text.x=element_text(angle=90, size=10, vjust=0.5))+
theme(legend.title = element_blank())+
theme(axis.ticks.y = element_blank(),axis.text.y = element_blank())+
xlab("Event Types")+
ylab("Economical Losses (USD)")+
ggtitle("The types of events with the greatest economic consequences in USA")+
theme(plot.title = element_text(size = 15,lineheight = 0.3,vjust=0.5))+
coord_cartesian(ylim = c(0,3500000))
print(g)