Where have I been in 2018

Last year I made a post about where I have been in 2017. I was worse with R so I solved it with a combination of cleaning up the data with R, importing it into Qgis and finally edited it in HitFilms.

This time I just did it all in R. The difficult part this time was to get the Google API running for the import of Google maps. (You need to enable billing to get the whole thing running.) It was the first time I used the new ggAnimate. It is great and easy to use. Less of a hazzle then the last times I used it.

I could reuse some of last years code, so I was done quite fast. (not the greatest code tough.)


library(tidyverse)
library(jsonlite)
library(ggplot2)
library(ggmap)
library(gganimate)
library(gifski)
library(zoo)
library(lubridate)

register_google(key = “AIzaSyCTCk3yYCPEo1UKVkZm_iQk_r4wPJCHlA4”)

system.time(x <- fromJSON(“GoogleLoc.json”))

# extracting the locations dataframe
loc = x$locations

# converting time column from posix milliseconds into a readable time scale
loc$time = as.POSIXct(as.numeric(x$locations$timestampMs)/1000, origin = “1970-01-01”)

# converting longitude and latitude from E7 to GPS coordinates
loc$lat = loc$latitudeE7 / 1e7
loc$lon = loc$longitudeE7 / 1e7

# calculate the number of data points per day, month and year
loc$date <- as.Date(loc$time, ‘%Y/%m/%d’)
loc$year <- year(loc$date)
loc$month_year <- as.yearmon(loc$date)

#new dataframe with the important units
maps<- data.frame(loc$lat,loc$long,loc$date,loc$time,loc$year)

#filter out the year and convert the longitude to the proper unit.
maps1<-maps%>%filter(loc.year==2018) %>% mutate(longitude = loc.long/10^7)

#choose the 10. measurement of each day. not very elegant, but good enough.
maps2<- maps1 %>% group_by(loc.date) %>%
summarise(long=(longitude[10]),
lat=(loc.lat[10]))

#get background map. set size, zoom, kind of map)
mamap <- get_map(location=c(mean(maps2$long,na.rm=T),mean(maps2$lat,na.rm=TRUE)+3), maptype = “satellite”,zoom=5)

#put it all together.
ggmap(mamap)+
geom_point(data=maps2,aes(x=long,y=lat),size=4, col=”red”)+
geom_label(data=maps2,x=1.5,y=56,aes(label=format(as.Date(loc.date),format=”%d.%m”)),size=10,col=”black”)+
theme_void()+
#the animation part
transition_time(loc.date)+
shadow_trail(alpha=0.3,colour=”#ff695e”,size=2,max_frames = 6)

a=animate(m, renderer = ffmpeg_renderer(),duration=20)
anim_save(filename = “my2018/2018video.mp4”)

The Trams of Zurich animated

After I saw a great post on /r/dataisbeautiful where someone mapped a place with the help of location-data of rented bicycles, I searched if there is some similar data available where I live.

Continue reading “The Trams of Zurich animated”