Last active
October 23, 2019 16:07
-
-
Save hrbrmstr/5ee3123f7161580d4cdf to your computer and use it in GitHub Desktop.
Rmd & css for https://beta.rstudioconnect.com/hrbrmstr/ggiraphe/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
text { font-family:"Helvetica"; } | |
h4 { padding-top:0; border-bottom:1px solid white; } | |
.tipspanstyle { | |
font-size:14px; | |
font-weight:700; | |
clear:right; | |
padding-bottom:10px; | |
} | |
.tipchart { text-align:left; } | |
.tiprow { vertical-align:middle; } | |
.tipbar { | |
background-color:steelblue; | |
font:9px sans-serif; | |
text-align:left; | |
padding:3px; | |
margin:1px; | |
line-height:10px; | |
color:white; | |
} | |
.tipbarticks { | |
font: 9px sans-serif; | |
text-align:right; | |
padding-right:3px; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "ggiraph with bar chart tooltips" | |
author: "hrbrmstr" | |
date: "`r Sys.Date()`" | |
output: | |
html_document: | |
css: ggiraphe.css | |
--- | |
The full `Rmd` and `css` files for this are in <a target=_blank href="https://gist.github.com/hrbrmstr/5ee3123f7161580d4cdf">this gist</a>. | |
This has been refactored a bit since the initial version (it now uses CSS classes vs inline CSS styles). | |
```{r message=FALSE} | |
library(ggplot2) | |
library(ggalt) | |
library(ggiraph) | |
library(ggthemes) | |
library(viridis) | |
library(stringi) | |
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"> | |
<h4>%s</h4> | |
<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>', | |
stri_trans_totitle(crimes$state), | |
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) | |
``` | |
```{r bib, include=FALSE} | |
# KEEP THIS AT THE END OF THE DOCUMENT TO GENERATE A LOCAL bib FILE FOR PKGS USED | |
knitr::write_bib(sub("^package:", "", grep("package", search(), value=TRUE)), file='skeleton.bib') | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment