Summer Road Trips – Light Blue circles indicate National Parks
My Location Heat Map While Attending Graduate School – Can you tell where I lived each of the two years? It was NOT on campus…
My Overall Heatmap of Austin
I have been using Google Timeline to track my location since 2013, and have finally gotten around to figuring out what to do with it. Using RStudio and the “jsonlite” and “plyr” packages, we can organize and display this data in more aesthetically pleasing ways than the simple timeline view google gives us.
‘The first step is to install the packages in RStdio
install.packages("jsonlite") install.packages("plyr") library(jsonlite) library(plyr)
data <- fromJSON("location.json")
import your JSON file, file name is “location.json” in my case
locs <- data$locations names(locs) ldf <- data.frame(t=rep(0,nrow(locs))) # Time is in POSIX * 1000 (milliseconds) format, convert it to useful scale... ldf$t <- as.numeric(locs$timestampMs)/1000 class(ldf$t) <- 'POSIXct' # Convert longitude and lattitude ldf$lat <- as.numeric(locs$latitudeE7/1E7) ldf$lon <- as.numeric(locs$longitudeE7/1E7)
Now that our data is in a useful form, we can turn the dataframe into a map. From here on out, your code may and should be different in some parts.
require(ggplot2) #install these packages if you haven't done so require(ggmap) ldfSummer <- ldf[29627:153800,]
Here in this code above, I am selecting a particular range of rows from my dataframe [29627:153800,] that represent Summer 2017, to create a map of just this summer. I do this again later in the code, to subset a different timeframe.
ldfGradSchool <- ldf[10438:848405,] ldfGradSchool$ID <- 1:nrow(ldfGradSchool) ldfSummer$ID <- 1:nrow(ldfSummer) #creating a new column for creating a gradient of colors based on time (not actual timestamp / not accurate) Austin <- get_map(c(-97.7431,30.2672), zoom = 13, source='stamen',maptype="watercolor") #watercolor, terrain, toner are options
Stamen is a really cool group that makes map-type tiles. http://maps.stamen.com/
Campus <- get_map(c(-97.737658, 30.286248), zoom = 15, source = 'stamen', maptype = "toner") USA <- get_map(c(-108.239,40.410), zoom = 4, source = 'stamen', maptype = "watercolor") USA <- get_map(c(-108.239,40.410), zoom = 4, source = 'stamen', maptype = "terrain") #depending on what map type you want
When making your map, you will need to experiment with the zoom level, the GPS coordinates for the center, the alpha level (opacity of the point), the maptype, and probably much more.
ggmap(Austin) + geom_point(data=ldf,aes(lon,lat),color="SteelBlue", alpha = .01) + ggtitle("Austin, Tx") + xlab(" ") + ylab(" ") ggmap(Austin, darken = c(0.45, "white")) + geom_point(data=ldfGradSchool,aes(lon,lat),color = "RoyalBlue", alpha = .12, size = 1) #GradSchoolAustin #darken function lowers the opacity of the map ggmap(Campus) + geom_point(data=ldfGradSchool,aes(lon,lat),color = "SteelBlue", alpha = .15, size = 1) #GradSchoolCampus ggmap(USA) + geom_point(data=ldfSummer,aes(lon,lat),color="SteelBlue", alpha = .03, size = 4) + ggtitle("Summer") + xlab(" ") + ylab(" ") + geom_point(data = nattyparks, aes(lon, lat), color = "skyblue", size =4, alpha= .8, shape = 1, stroke = 3) # Trip
All of these ggmap snippets are all examples of maps I created using this.
This project was made possible by StackExchange, Google, Stamen, and this original post by Rob
Download the R code itself here
Map tiles by Stamen Design under CC BY 3.0
http://maps.stamen.com/#watercolor/12/37.7706/-122.3782
“Map data by OpenStreetMap, under ODbL.”
Used under the creative commons license. https://creativecommons.org/licenses/by/3.0/
Adaptions were made to the original Maps.
Awesome!
One thing: When selecting your date ranges, you could simplify it by subseting:
ldfSummer =”2017-06-20″ & t<="2017-09-20"]
Or so.
LikeLike
The timestamp has a specific time along with the date. I didn’t want to mess around dropping the hours, minutes, and seconds. That would have worked too though!
LikeLike
Super Cool! I’m trying to do this with my own data.
I did want to ask why RStudio would be refusing the JSON, I’m currently having issues importing the JSON from my desktop. Any pointers for a noobie?
LikeLike
You must have installed, and be running the “jsonlite” package (2 different commands). You also want to make sure your working directory is set to your desktop.
LikeLiked by 1 person
Gotcha, I’ve verified that “jsonlite” is installed and running, and that my working directory is set up, but when I run the first command its throwing me an “Error in fromJSON(“location.json”) : unexpected character ‘l'” error. Does the JSON need to be edited in anyway or is this still a problem with fromJSON finding the file?
LikeLiked by 1 person
Is your file names “location.json”? Your best bet and one I’ve used a multitude of times is Stack Exchange. Check it out!
LikeLike
Alright, thanks, I’ll try my luck there. Thanks for the help and I love the project, can’t wait to get it working myself!
LikeLike