Sunday, September 20, 2015
Some Fun with googleVis - Mapping Blog Visits on Google Map
See stand-alone code to produce this map below.require(RJSONIO)
require(RCurl)
require(plyr)
library(googleVis)
# function to retrieve coords from ip-addresses
# (https://github.com/rtelmore/RDSTK/blob/master/src/RDSTK/R/functions.R)
ip2coordinates <- function(ip, session=getCurlHandle()) {
api <- "http://www.datasciencetoolkit.org/ip2coordinates/"
get.ips <- getURL(paste(api, URLencode(ip), sep=""), curl=session)
result <- ldply(fromJSON(get.ips), data.frame)
names(result)[1] <- "ip.address"
return(result)
}
# read log-file:
setwd(tempdir())
download.file("http://docs.google.com/uc?export=download&id=0B2wAunwURQNsZjY4Njc1OTYtYWE3My00ZjNjLTg1YzEtNDNlNmYyYmNiYmI0",
destfile = "google_docs.csv", mode = "wb")
log <- read.csv(paste(tempdir(),"/", "google_docs.csv", sep = ""),
header = T, stringsAsFactors = F)
# create dataframe to collect coords:
nr = nrow(log)
Lon <- as.numeric(rep(NA, nr))
Lat <- Lon
Coords <- data.frame(Lon, Lat)
# some will not be found (I will dismiss these rows later):
for (i in 1:nr){
try(
Coords[i, 1:2] <- ip2coordinates(log$IP.Address[i])[c("longitude", "latitude")]
)
}
# append to log-file:
log <- data.frame(log, Lat = Coords$Lat, Long = Coords$Lon,
LatLong = paste(round(Coords$Lat, 1),
round(Coords$Lon, 1),
sep = ":"))
log_gmap <- log[!is.na(log$Lat), ]
gmap <- gvisMap(log_gmap, "LatLong",
options = list(showTip = TRUE, enableScrollWheel = TRUE,
mapType = 'hybrid', useMapTypeControl = TRUE,
width = 550, height = 300))
plot(gmap)
Labels:
Fun,
Google Maps,
googleVis,
IP-Address,
Maps,
R
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment