Skip to content

Instantly share code, notes, and snippets.

@kzktmr
Last active December 11, 2015 14:30
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 kzktmr/0f8d492057d577411042 to your computer and use it in GitHub Desktop.
Save kzktmr/0f8d492057d577411042 to your computer and use it in GitHub Desktop.
Draw choropleth map of Japan using (deprecated) Google Chart API from R
# This function uses an old API. It is not recommended to use this function.
googleChartJapanMap <- function(x, width=500, height=600, col.def="gray", col.min="lightgreen", col.max="darkgreen", col.bg="white", file=NULL, title=NULL){
#2010/12/28 ver.0.1 by kzktmr
#2015/12/11 ver.0.2 by kzktmr
stopifnot(length(x)==47, width*height<=300000)
col2hex <- function(col){
col.rgb <- col2rgb(col)
return(substring(rgb(col.rgb[1], col.rgb[2], col.rgb[3], maxColorValue=255), 2))
}
xx <- x[!is.na(x)]
http <- "http://chart.apis.google.com/chart?cht=map"
chld <- paste("JP-", sprintf("%02d", 1:47), sep="")[!is.na(x)]
chld <- paste("chld=", paste(chld, collapse="|"), sep="")
chco <- paste("chco=", paste(col2hex(col.def), col2hex(col.min), col2hex(col.max), sep=","), sep="")
chd <- paste("chd=t:", paste(xx, collapse=","), sep="")
chds <- paste("chds=", min(xx), ",", max(xx), sep="")
chs <- paste("chs=", width, "x", height, sep="")
chf <- paste("chf=bg,s,", col2hex(col.bg), sep="")
myurl <- paste(http, chld, chco, chd, chds, chs, chf, sep="&")
if(!is.null(title)){
chtt <- paste("chtt=", gsub(" ", "+", title), sep="")
myurl <- paste(myurl, chtt, sep="&")
}
if(is.null(file)){
if(.Platform$OS.type=="windows")writeClipboard(myurl, format=1)
if(Sys.info()["sysname"]=="Darwin")write(myurl, pipe("pbcopy"))
}else{
download.file(url=myurl, destfile=file, mode = "wb")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment