Mapping 2013 US weather events damage

This project for the Coursera data products course uses NOAA data to map out deaths, injuries and dollar damage across the US from weather events.

Data wrangling

# reading the data
dataRaw <- read.csv("stormdata_2013.csv")

# filling empty damage values with 0's
dataRaw$DAMAGE_CROPS[dataRaw$DAMAGE_CROPS == ""] <- "0.00K"
dataRaw$DAMAGE_PROPERTY[dataRaw$DAMAGE_PROPERTY == ""] <- "0.00K"

# helper function to expand abbreviated damage dollar value
dollarNum <- function(KM) {
        modC <- substr(KM, nchar(KM), nchar(KM))
        modN <- case_when(
                modC == "K" ~ 1e3,
                modC == "M" ~ 1e6,
                modC == "B" ~ 1e9,
                .default = 1
        )
        num <- as.numeric(substr(KM, 1, nchar(KM)-1))
        num * modN
}

# summarizing impacts by state
dataSum <- dataRaw %>% 
        group_by(STATE) %>%
        summarize(
                events = n(),
                injuries = sum(INJURIES_DIRECT) + sum(INJURIES_INDIRECT),
                deaths = sum(DEATHS_DIRECT) + sum(DEATHS_INDIRECT),
                damage = sum(dollarNum(DAMAGE_PROPERTY)) + sum(dollarNum(DAMAGE_CROPS))
        )

# converting full uppercase state names to abbreviation used by plotly choropleth
dataSum$STATE <- tools::toTitleCase(tolower(dataSum$STATE))
dataSum$stateAbb <- state.abb[match(dataSum$STATE, state.name)]
dataSum$hover <- with(dataSum, paste(STATE, ":", events, "weather events <br>",
                                        injuries, "injuries,", deaths, "deaths <br> $",
                                        damage, "in damage to property and crops"))
dataSum <- dataSum %>% 
        select(stateAbb, events, hover) %>%
        filter(!is.na(stateAbb))

Plotly map

borders <- list(color = toRGB("black"))

map_options <- list(
        scope = 'usa',
        projection = list(type = 'albers usa'),
        showlakes = T
)

dataSum <- as.data.frame(dataSum)

plot_ly(dataSum, z = dataSum$events, text = dataSum$hover, locations = dataSum$stateAbb,
        type = 'choropleth', locationmode = 'USA-states',
        color = dataSum$events, colors = 'Reds', marker = list(line = borders)) %>%
layout(title = 'US weather events impacts for 2013', geo = map_options)

Cleanup

rm(list=ls())