Skip to content

Instantly share code, notes, and snippets.

@shawngraham
Created December 18, 2015 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawngraham/5a036bc0477dbae48b91 to your computer and use it in GitHub Desktop.
Save shawngraham/5a036bc0477dbae48b91 to your computer and use it in GitHub Desktop.
# making a network diagram with xkcd font etc
# nb xkcd package can do some pretty awesome stuff; see https://cran.r-project.org/web/packages/xkcd/xkcd.pdf
# what I'm doing here doesn't even scratch the surface.
# the networkD3 package makes the network; I think to use the full utility of xkcd package, I'd have to build my network differently, perhaps as separate node and edge lists; see this tutorial http://kateto.net/network-visualization
# the upside to the networkD3 package is that I know have Andrew Reinhard's archaeogaming map as a json file at
# https://gist.github.com/shawngraham/2bfad0c10e7519a630a9 so you can use it for other stuff.
# anyway
# first we have to get set up
#install.packages("networkD3")
library(networkD3)
#install.packages("xkcd")
library(xkcd)
library(extrafont)
download.file("http://simonsoftware.se/other/xkcd.ttf", dest="xkcd.ttf", mode="wb")
system("mkdir ~/.fonts")
system("cp xkcd.ttf ~/.fonts")
font_import(paths = "~/.fonts", pattern = "[X/x]kcd", prompt=FALSE)
fonts()
fonttable()
if(.Platform$OS.type != "unix") {
## Register fonts for Windows bitmap output
loadfonts(device="win")
} else {
loadfonts()
}
## At this point, you also have to install the fonts; for some reason the code above doesn't do everything that is necessary. I'm on a Mac. I just did a spotlight search for xkcd.ttf, it found it, with a button, 'install fonts'. So I clicked it.
if( 'xkcd' %in% fonts()) {
p <- ggplot() + geom_point(aes(x=mpg, y=wt), data=mtcars) + theme (text = element_text(size = 16, family = "xkcd"))
} else {
warning("Not xkcd fonts installed!")
p <- ggplot() + geom_point(aes(x=mpg, y=wt), data=mtcars)
}
p
# And so we know that XKCD package works!
### so let's go get our json
URL <- paste0(
"https://gist.githubusercontent.com/shawngraham/2bfad0c10e7519a630a9/raw/",
"7a7a2da8c4358a81e0dc05cc64405d7c9f975803/archaeomap-flare.json")
## Convert to list format
Flare <- jsonlite::fromJSON(URL, simplifyDataFrame = FALSE)
## plot it
radialNetwork(List = Flare, fontSize = 10, opacity = 0.9)
diagonalNetwork(List = Flare, fontSize = 10, fontFamily = "xkcd", opacity = 0.9, width = 500)
## As I said, you'd have to build a network diagram using the regular graphic packages I think to get the full XKCD experience, including the stick figures etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment