#Loading the libraries library(reshape2) library(reshape) library(ggplot2) library(plyr) library(dplyr) setwd(“~/Desktop/Training/Data Science-Coursera/5 /Week 4/Peer-graded Assignment_Course Project 2”) storm_data <- read.csv(“StormData.csv.bz2”, header = T) total_health <- aggregate(cbind(FATALITIES, INJURIES)~EVTYPE, data=storm_data, sum, na.rm=TRUE) total_health_factor <- melt(total_health[order(-(total_health\(FATALITIES + total_health\)INJURIES)),][1:10,], id.vars = “EVTYPE”) names(total_health_factor) <- c(“EVTYPE”,“CAUSE”,“COUNT”) storm_data <- transform(storm_data, PROPDMG = ifelse(PROPDMGEXP %in% “B”, PROPDMG10^9, ifelse(PROPDMGEXP %in% c(“m”, “M”), PROPDMG10^6, ifelse(PROPDMGEXP %in% c(“k”, “K”), PROPDMG10^3, ifelse(PROPDMGEXP %in% c(“h”, “H”), PROPDMG100, PROPDMG))))) property_damage <- aggregate(PROPDMG~EVTYPE, data=storm_data, sum, na.rm = T) top_list <- property_damage[order(property_damage$PROPDMG, decreasing = T),][1:10,]

ggplot(total_health_factor, aes(x = reorder(EVTYPE, COUNT), y = COUNT, fill = CAUSE)) +
geom_bar(stat = “identity”) + coord_flip() + scale_y_continuous(breaks = seq(0, 100000, by = 2500)) + ylab(“Total injuries and fatalities”) + xlab(“Event type”) + ggtitle(“The 10 most harmful events with respect to population health”) + theme(axis.text.x = element_text(angle = 60, hjust = 1))

ggplot(top_list, aes(x = reorder(EVTYPE, PROPDMG/10e9), y = PROPDMG/10e9)) + geom_bar(stat = “identity”, fill = “darkturquoise”) + coord_flip() + ylab(“Property damage in billions of dollars”) + xlab(“Event type”) + ggtitle(“The 10 most harmful events with respect to property damage”)