Peter Ellis has recently published a blog post on Deaths from assault over time in 40 relatively rich countries
He produces charts of trends over time (downward recently), victim gender differences (yes more men get killed everywhere) and looks at possible relationship to predominantly Catholic countries (it’s complicated) Anyways with attached code it is well worth a read
With my current interest in the newly-open sourced plotly library maded available via an R package by the folks at ropenScience I thought it was worth adding a couple of extras based on Peter’s grunt work
# additional libraries
library(plotly)
library(leaflet)
library(rgdal)
library(tigris)
viol_spread %>%
rename(All=Population) %>%
mutate(Female=round(Female,2),Male=round(Male,2),All=round(All,2)) %>%
arrange(All) %>%
gather(Gender, value, Female,All, Male) %>%
plot_ly(x = value, y = Country, mode = "markers", hoverinfo="text",
text = paste(Country,"<br>",value,"per 100,000 pop"),
color = Gender, colors = c("pink","green","blue")) %>%
add_trace(x = value, y = Country, mode = "lines",
group = Country, showlegend = F, line = list(color = "gray", width=1)) %>%
layout(hovermode = "closest", autosize= F, width=800, height= 1000,
title = "Mean Annual deaths from Assualt 1990-2013, by gender",
xaxis = list(type="log", title = "Deaths per 100,000 (logarithmic scale)",tick0=1,dtick=1),
yaxis = list(title = ""),
margin = list(l = 120)
)
Plotly charts provide the ability to pan and zoom, but I nevertheless found using a logx-axis the best option, leaving the tooltip (accessed by hovering over points) to show actual rates
# read in previously obtained standard shape files
maps <- readOGR(
dsn = ".",
layer = "ne_50m_admin_0_countries",
encoding = "UTF-8",verbose = FALSE
)
#Restrict to latest year with virtually all countries data available
viol2010 <-
viol %>%
filter(Year==2010&Unit=="Deaths per 100 000 population")
# correct for different names for countries
## setdiff(viol2010$Country,as.character(maps$name)) #[1] "Czech Republic" "Korea, Republic of" "Russian Federation"
viol2010[viol2010$Country=="Czech Republic",]$Country <- "Czech Rep."
viol2010[viol2010$Country=="Korea, Republic of",]$Country <- "Korea"
viol2010[viol2010$Country=="Russian Federation",]$Country <- "Russia"
# Set colour range
pal <- colorNumeric("Reds", c(0,max(viol2010$Value)))
#v join using tigris package HT Kyle Walker
countries <- geo_join(
maps,
viol2010,
"name",
"Country"
)
# add a popup field
countries$popUp <-
paste0(
"<strong>",countries$name,"</strong><br>", countries$Value," per 100,000"
)
# create map
countries %>%
leaflet() %>%
addTiles() %>%
addPolygons(
fillColor = ~ pal(Value),
fillOpacity = 0.6,
color = "#BDBDC3",
weight = 1,
popup = ~ popUp
) %>%
mapOptions(zoomToLimits = "first") %>%
addLegend(pal = pal,
values = countries$Value,
position = "bottomright",
title = "Deaths from Assualts 2010",
labFormat = labelFormat(suffix = " per 100,000 popn."))
The chart is based on 2010 data, which includes 39 countries. Click country for details.
If I get round to a shiny version on the mainlyMaps section of my dashboard site there will be room for more interactivity