The full Rmd and css files for this are in this gist.
This has been refactored a bit since the initial version (it now uses CSS classes vs inline CSS styles).
library(ggplot2)
library(ggalt)
library(ggiraph)
library(ggthemes)
library(viridis)
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
crimes$onclick <- sprintf(
"function() {window.open('%s%s')}",
"http://en.wikipedia.org/wiki/",
as.character(crimes$state)
)
max_bar <- max(crimes$Murder, crimes$Assault, crimes$Rape)
crimes$tip <- sprintf('
<div class="tipchart">
<span class="tipspanstyle">Arrests per crime per 100K</span>
<table>
<tr class="tiprow">
<td class="tipbarticks">Murder</td>
<td class="tipbardiv"><div class="tipbar" style="width:%dpx;">%3.1f</div></td>
</tr>
<tr class="tiprow">
<td class="tipbarticks">Rape</td>
<td class="tipbardiv"><div class="tipbar" style="width:%dpx;">%3.1f</div></td>
</tr>
<tr class="tiprow">
<td class="tipbarticks">Assault</td>
<td class="tipbardiv"><div class="tipbar" style="width %dpx;">%3.1f</div></td>
</tr>
</table>
</div>',
round((crimes$Murder/(max_bar/1.25)*100)), crimes$Murder,
round((crimes$Rape/(max_bar/1.25)*100)), crimes$Rape,
round((crimes$Assault/(max_bar/1.25)*100)), crimes$Assault)
# javascript is too dumb to deal with line breaks in strings well
crimes$tip <- gsub("\\\n", "", crimes$tip)
states_map <- map_data("state")
gg <- ggplot()
gg <- gg + geom_map_interactive(
map=states_map, data=crimes,
aes(fill=Murder, tooltip=tip, onclick=onclick, use_jquery=TRUE,
data_id=state, map_id=state),
color="white", size=0.15)
gg <- gg + scale_fill_viridis(name="Murder arrest rate \n(per 100K) ")
gg <- gg + coord_proj("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96")
gg <- gg + ggtitle("US Arrests (1973)")
gg <- gg + expand_limits(x=states_map$long, y=states_map$lat)
gg <- gg + theme_map(base_family="Helvetica")
gg <- gg + theme(legend.position="bottom")
gg <- gg + theme(plot.title=element_text(hjust=0.5, size=24, family="Helvetica"))
gg <- gg + theme(legend.title=element_text(family="Helvetica"))
gg <- gg + theme(legend.title.align=1)
ggiraph(code = {print(gg)}, width = 10, height = 6)