You can recreate CS 10 or find your own spatial data to display using leaflet. I decided to use my own spatial data which I downloaded from Federal Railroad Administration Office of Safety Analysis website. The data contains all railroad accidents that involved casualties from 2018 in the United States.
# Read in 2018 casualty data
casualty_2018 <- read_csv("C:/Users/User/Documents/Fall 2019/Math 335/Project/casualty_2018.csv") %>%
select(IYR,IMO,RAILROAD,TYPPERS,AGE,STATE,TYPRR,REGION,CASFATAL,DAY,YEAR4,TIMEHR,TIMEMIN,AMPM,COUNTY,CNTYCD,STCNTY,
HZMEXPOS,TERMINAT,NARR1,NARR2,LATITUDE,LONGITUD) %>%
mutate(Fatal = case_when(CASFATAL == "Y" ~ 1,
CASFATAL == "N" ~ 0)) %>%
filter(Fatal == 1)
# Create death icon for the map
deathicon <- iconList( Y = makeIcon("C:/Users/User/Documents/Fall 2019/Math 335/Project/death_icon.jpg",
"C:/Users/User/Documents/Fall 2019/Math 335/Project/death_icon.jpg", 24, 24))
# Which State had most deaths
state_2018 <- casualty_2018 %>%
mutate(state_name = fips(STATE, to = "Name")) %>%
group_by(state_name) %>%
summarize(deaths = sum(Fatal))
# Create map of fatalities in US
leaflet(casualty_2018) %>% setView(lng = -98, lat = 39, zoom = 3.5) %>% addTiles() %>%
addMarkers(lng = ~LONGITUD, lat = ~LATITUDE, icon = ~deathicon[CASFATAL])
The plot above shows the locations of each of 832 deaths that occured from trains in 2018. California had more than three times as many deaths as the next closest state at 160. Alaska, Maine, and South Dakota all tied for the least deaths with only one in each state. As can be seen on the graph the majority of train deaths appear to happen on the eastern half of the United States.
# Create a datatable showing data
datatable(state_2018)