<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/how-to-map-your-google-location-history-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shiring.github.io/maps/2016/12/30/Standortverlauf_post"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It’s no secret that Google Big Brothers most of us. But at least they allow us to access quite a lot of the data they have collected on us. Among this is the Google location history.</p>
<p>If you want to see a few ways how to quickly and easily visualize your location history with R, stay tuned…</p>
<p>The Google location history can be downloaded from your Google account under <a target="_blank" rel="nofollow" href="https://takeout.google.com/settings/takeout">https://takeout.google.com/settings/takeout</a>. Make sure you only tick <em>“location history”</em> for download, otherwise it will take super long to get all your Google data.</p>
<p>The data Google provides you for download is a .json file and can be loaded with the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/jsonlite/index.html">jsonlite</a> package. Loading this file into R might take a few minutes because it can be quite big, depending on how many location points Google had saved about you.</p>
<div>
<pre><code><span>library</span><span>(</span><span>jsonlite</span><span>)</span><span>
</span><span>system.time</span><span>(</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>fromJSON</span><span>(</span><span>&quot;Standortverlauf.json&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##    user  system elapsed 
##  112.52    1.12  132.59
</code></pre>
</div>
<p></p>
<p>The date and time column is in the POSIX milliseconds format, so I converted it to human readable POSIX.</p>
<p>Similarly, longitude and latitude are saved in E7 format and were converted to GPS coordinates.</p>
<div>
<pre><code><span># extracting the locations dataframe
</span><span>loc</span><span> </span><span>=</span><span> </span><span>x</span><span>$</span><span>locations</span><span>
</span><span># converting time column from posix milliseconds into a readable time scale
</span><span>loc</span><span>$</span><span>time</span><span> </span><span>=</span><span> </span><span>as.POSIXct</span><span>(</span><span>as.numeric</span><span>(</span><span>x</span><span>$</span><span>locations</span><span>$</span><span>timestampMs</span><span>)</span><span>/</span><span>1000</span><span>,</span><span> </span><span>origin</span><span> </span><span>=</span><span> </span><span>&quot;1970-01-01&quot;</span><span>)</span><span>
</span><span># converting longitude and latitude from E7 to GPS coordinates
</span><span>loc</span><span>$</span><span>lat</span><span> </span><span>=</span><span> </span><span>loc</span><span>$</span><span>latitudeE7</span><span> </span><span>/</span><span> </span><span>1e7</span><span>
</span><span>loc</span><span>$</span><span>lon</span><span> </span><span>=</span><span> </span><span>loc</span><span>$</span><span>longitudeE7</span><span> </span><span>/</span><span> </span><span>1e7</span><span>
</span></code></pre>
</div>
<p></p>
<p>This is how the data looks like now:</p>
<div>
<pre><code><span>head</span><span>(</span><span>loc</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##     timestampMs latitudeE7 longitudeE7 accuracy                 activitys
## 1 1482393378938  519601402    76004708       29                      NULL
## 2 1482393333953  519601402    76004708       29                      NULL
## 3 1482393033893  519603616    76002628       20 1482393165600, still, 100
## 4 1482392814435  519603684    76001572       20 1482392817678, still, 100
## 5 1482392734911  519603684    76001572       20                      NULL
## 6 1482392433788  519603684    76001572       20                      NULL
##   velocity heading altitude                time      lat      lon
## 1       NA      NA       NA 2016-12-22 08:56:18 51.96014 7.600471
## 2       NA      NA       NA 2016-12-22 08:55:33 51.96014 7.600471
## 3       NA      NA       NA 2016-12-22 08:50:33 51.96036 7.600263
## 4       NA      NA       NA 2016-12-22 08:46:54 51.96037 7.600157
## 5       NA      NA       NA 2016-12-22 08:45:34 51.96037 7.600157
## 6       NA      NA       NA 2016-12-22 08:40:33 51.96037 7.600157
</code></pre>
</div>
<p>We have the original and converted time, latitude and longitude columns, plus accuracy, activities, velocity, heading and altitude. Accuracy gives the error distance around the point in metres. Activities are saved as a list of data frames and will be explored further down. Velocity, heading and altitude were not recorded for earlier data points.</p>
<p></p>
<h1>Data stats</h1>
<p>Before I get to actually plotting maps, I want to explore a few basic statistics of the data.</p>
<p></p>
<h2>How many data points did Google record over what period of time?</h2>
<div>
<pre><code><span># how many rows are in the data frame?
</span><span>nrow</span><span>(</span><span>loc</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 600897
</code></pre>
</div>
<div>
<pre><code><span>min</span><span>(</span><span>loc</span><span>$</span><span>time</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;2013-09-06 19:33:41 CEST&quot;
</code></pre>
</div>
<div>
<pre><code><span>max</span><span>(</span><span>loc</span><span>$</span><span>time</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;2016-12-22 08:56:18 CET&quot;
</code></pre>
</div>
<p></p>
<h2>And how are they distributed over days, months and years?</h2>
<div>
<pre><code><span># calculate the number of data points per day, month and year
</span><span>library</span><span>(</span><span>lubridate</span><span>)</span><span>
</span><span>library</span><span>(</span><span>zoo</span><span>)</span><span>
</span><span>loc</span><span>$</span><span>date</span><span> </span><span>&lt;-</span><span> </span><span>as.Date</span><span>(</span><span>loc</span><span>$</span><span>time</span><span>,</span><span> </span><span>'%Y/%m/%d'</span><span>)</span><span>
</span><span>loc</span><span>$</span><span>year</span><span> </span><span>&lt;-</span><span> </span><span>year</span><span>(</span><span>loc</span><span>$</span><span>date</span><span>)</span><span>
</span><span>loc</span><span>$</span><span>month_year</span><span> </span><span>&lt;-</span><span> </span><span>as.yearmon</span><span>(</span><span>loc</span><span>$</span><span>date</span><span>)</span><span>
</span><span>points_p_day</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>table</span><span>(</span><span>loc</span><span>$</span><span>date</span><span>),</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>&quot;day&quot;</span><span>)</span><span>
</span><span>points_p_month</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>table</span><span>(</span><span>loc</span><span>$</span><span>month_year</span><span>),</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>&quot;month&quot;</span><span>)</span><span>
</span><span>points_p_year</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>table</span><span>(</span><span>loc</span><span>$</span><span>year</span><span>),</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>&quot;year&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<h3>How many days were recorded?</h3>
<div>
<pre><code><span>nrow</span><span>(</span><span>points_p_day</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 1057
</code></pre>
</div>
<h3>How many months?</h3>
<div>
<pre><code><span>nrow</span><span>(</span><span>points_p_month</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 39
</code></pre>
</div>
<h3>And how many years?</h3>
<div>
<pre><code><span>nrow</span><span>(</span><span>points_p_year</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 4
</code></pre>
</div>
<div>
<pre><code><span># set up plotting theme
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggmap</span><span>)</span><span>
</span><span>my_theme</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>base_family</span><span> </span><span>=</span><span> </span><span>&quot;sans&quot;</span><span>){</span><span>
</span><span>theme_grey</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>base_size</span><span>,</span><span> </span><span>base_family</span><span> </span><span>=</span><span> </span><span>base_family</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>),</span><span>
</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>14</span><span>),</span><span>
</span><span>panel.grid.major</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>),</span><span>
</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;aliceblue&quot;</span><span>),</span><span>
</span><span>strip.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;lightgrey&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>),</span><span>
</span><span>strip.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;navy&quot;</span><span>),</span><span>
</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>,</span><span>
</span><span>legend.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.margin</span><span> </span><span>=</span><span> </span><span>unit</span><span>(</span><span>.5</span><span>,</span><span> </span><span>&quot;lines&quot;</span><span>),</span><span>
</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>NA</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span>
</span><span>)</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>points</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>points_p_day</span><span>[,</span><span> </span><span>-1</span><span>],</span><span> </span><span>points_p_month</span><span>[,</span><span> </span><span>-1</span><span>],</span><span> </span><span>points_p_year</span><span>[,</span><span> </span><span>-1</span><span>])</span><span>
</span><span>ggplot</span><span>(</span><span>points</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>group</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Freq</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>position_jitter</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>0.2</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_boxplot</span><span>(</span><span>aes</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>group</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>outlier.colour</span><span> </span><span>=</span><span> </span><span>NA</span><span>)</span><span> </span><span>+</span><span> </span><span>facet_grid</span><span>(</span><span>group</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>
</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;none&quot;</span><span>,</span><span>
</span><span>strip.placement</span><span> </span><span>=</span><span> </span><span>&quot;outside&quot;</span><span>,</span><span>
</span><span>strip.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>strip.text</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Number of data points&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;How many data points did Google collect about me?&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Number of data points per day, month and year&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;\nGoogle collected between 0 and 1500 data points per day
(median ~500), between 0 and 40,000 per month (median ~15,000) and between 80,000 and 220,000 per year (median ~140,000).&quot;</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i1.wp.com/shiring.github.io/maps/2016/12/30/Standortverlauf_post_files/figure-markdown_github/unnamed-chunk-11-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<h2>How accurate is the data?</h2>
<p>Accuracy is given in meters, i.e. the smaller the better.</p>
<div>
<pre><code><span>accuracy</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>accuracy</span><span> </span><span>=</span><span> </span><span>loc</span><span>$</span><span>accuracy</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>loc</span><span>$</span><span>accuracy</span><span> </span><span>&lt;</span><span> </span><span>800</span><span>,</span><span> </span><span>&quot;high&quot;</span><span>,</span><span> </span><span>ifelse</span><span>(</span><span>loc</span><span>$</span><span>accuracy</span><span> </span><span>&lt;</span><span> </span><span>5000</span><span>,</span><span> </span><span>&quot;middle&quot;</span><span>,</span><span> </span><span>&quot;low&quot;</span><span>)))</span><span>
</span><span>accuracy</span><span>$</span><span>group</span><span> </span><span>&lt;-</span><span> </span><span>factor</span><span>(</span><span>accuracy</span><span>$</span><span>group</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;high&quot;</span><span>,</span><span> </span><span>&quot;middle&quot;</span><span>,</span><span> </span><span>&quot;low&quot;</span><span>))</span><span>
</span><span>ggplot</span><span>(</span><span>accuracy</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>accuracy</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>group</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_histogram</span><span>()</span><span> </span><span>+</span><span> </span><span>facet_grid</span><span>(</span><span>group</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>scales</span><span>=</span><span>&quot;free&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>
</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;none&quot;</span><span>,</span><span>
</span><span>strip.placement</span><span> </span><span>=</span><span> </span><span>&quot;outside&quot;</span><span>,</span><span>
</span><span>strip.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Accuracy in metres&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Count&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;How accurate is the location data?&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Histogram of accuracy of location points&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;\nMost data points are pretty accurate, but there are still many data points with a high inaccuracy.
These were probably from areas with bad satellite reception.&quot;</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i0.wp.com/shiring.github.io/maps/2016/12/30/Standortverlauf_post_files/figure-markdown_github/unnamed-chunk-12-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<h1>Plotting data points on maps</h1>
<p>Finally, we are actually going to plot some maps!</p>
<p>The first map is a simple point plot of all locations recorded around Germany.</p>
<p>You first specify the map area and the zoom factor (the furthest away is 1); the bigger the zoom, the closer to the center of the specified location. Location can be given as longitude/latitude pair or as city or country name.</p>
<p>On this map we can plot different types of plot with the regular ggplot2 syntax. For example, a point plot.</p>
<div>
<pre><code><span>germany</span><span> </span><span>&lt;-</span><span> </span><span>get_map</span><span>(</span><span>location</span><span> </span><span>=</span><span> </span><span>'Germany'</span><span>,</span><span> </span><span>zoom</span><span> </span><span>=</span><span> </span><span>5</span><span>)</span><span>
</span><span>ggmap</span><span>(</span><span>germany</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>loc</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>lon</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Longitude&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Latitude&quot;</span><span>,</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Location history data points in Europe&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;\nA simple point plot shows recorded positions.&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i1.wp.com/shiring.github.io/maps/2016/12/30/Standortverlauf_post_files/figure-markdown_github/unnamed-chunk-13-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<p>The second map shows a 2D bin plot of accuracy measured for all data points recorded in my home town Münster.</p>
<div>
<pre><code><span>munster</span><span> </span><span>&lt;-</span><span> </span><span>get_map</span><span>(</span><span>location</span><span> </span><span>=</span><span> </span><span>'Munster'</span><span>,</span><span> </span><span>zoom</span><span> </span><span>=</span><span> </span><span>12</span><span>)</span><span>
</span><span>options</span><span>(</span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>T</span><span>)</span><span>
</span><span>ggmap</span><span>(</span><span>munster</span><span>)</span><span> </span><span>+</span><span> </span><span>stat_summary_2d</span><span>(</span><span>geom</span><span> </span><span>=</span><span> </span><span>&quot;tile&quot;</span><span>,</span><span> </span><span>bins</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>loc</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>lon</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>z</span><span> </span><span>=</span><span> </span><span>accuracy</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span> </span><span>+</span><span> </span><span>scale_fill_gradient</span><span>(</span><span>low</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Accuracy&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Longitude&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Latitude&quot;</span><span>,</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Location history data points around Münster&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Color scale shows accuracy (low: blue, high: red)&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;\nThis bin plot shows recorded positions and their accuracy in and around Münster&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i0.wp.com/shiring.github.io/maps/2016/12/30/Standortverlauf_post_files/figure-markdown_github/unnamed-chunk-14-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<p>We can also plot the velocity of each data point:</p>
<div>
<pre><code><span>loc_2</span><span> </span><span>&lt;-</span><span> </span><span>loc</span><span>[</span><span>which</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>loc</span><span>$</span><span>velocity</span><span>)),</span><span> </span><span>]</span><span>
</span><span>munster</span><span> </span><span>&lt;-</span><span> </span><span>get_map</span><span>(</span><span>location</span><span> </span><span>=</span><span> </span><span>'Munster'</span><span>,</span><span> </span><span>zoom</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span>
</span><span>ggmap</span><span>(</span><span>munster</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>loc_2</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>lon</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>velocity</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</span><span>)</span><span> </span><span>+</span><span> </span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Longitude&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Latitude&quot;</span><span>,</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Location history data points in Münster&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Color scale shows velocity measured for location&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;\nA point plot where points are colored according to velocity nicely reflects that I moved generally slower in the city center than on the autobahn&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_colour_gradient</span><span>(</span><span>low</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Velocity&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<p><img src="https://i2.wp.com/shiring.github.io/maps/2016/12/30/Standortverlauf_post_files/figure-markdown_github/unnamed-chunk-15-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<h1>What distance did I travel?</h1>
<p>To obtain the distance I moved, I am calculating the distance between data points. Because it takes a long time to calculate, I am only looking at data from last year.</p>
<div>
<pre><code><span>loc3</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>loc</span><span>,</span><span> </span><span>subset</span><span>(</span><span>loc</span><span>,</span><span> </span><span>loc</span><span>$</span><span>time</span><span> </span><span>></span><span> </span><span>as.POSIXct</span><span>(</span><span>'2016-01-01 0:00:01'</span><span>)))</span><span>
</span><span>loc3</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>loc</span><span>,</span><span> </span><span>subset</span><span>(</span><span>loc3</span><span>,</span><span> </span><span>loc</span><span>$</span><span>time</span><span> </span><span>&lt;</span><span> </span><span>as.POSIXct</span><span>(</span><span>'2016-12-22 23:59:59'</span><span>)))</span><span>
</span><span># Shifting vectors for latitude and longitude to include end position
</span><span>shift.vec</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>vec</span><span>,</span><span> </span><span>shift</span><span>){</span><span>
</span><span>if</span><span> </span><span>(</span><span>length</span><span>(</span><span>vec</span><span>)</span><span> </span><span>&lt;=</span><span> </span><span>abs</span><span>(</span><span>shift</span><span>)){</span><span>
</span><span>rep</span><span>(</span><span>NA</span><span> </span><span>,</span><span>length</span><span>(</span><span>vec</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>if</span><span> </span><span>(</span><span>shift</span><span> </span><span>>=</span><span> </span><span>0</span><span>)</span><span> </span><span>{</span><span>
</span><span>c</span><span>(</span><span>rep</span><span>(</span><span>NA</span><span>,</span><span> </span><span>shift</span><span>),</span><span> </span><span>vec</span><span>[</span><span>1</span><span>:</span><span>(</span><span>length</span><span>(</span><span>vec</span><span>)</span><span> </span><span>-</span><span> </span><span>shift</span><span>)])</span><span> </span><span>}</span><span>
</span><span>else</span><span> </span><span>{</span><span>
</span><span>c</span><span>(</span><span>vec</span><span>[(</span><span>abs</span><span>(</span><span>shift</span><span>)</span><span> </span><span>+</span><span> </span><span>1</span><span>)</span><span>:</span><span>length</span><span>(</span><span>vec</span><span>)],</span><span> </span><span>rep</span><span>(</span><span>NA</span><span>,</span><span> </span><span>abs</span><span>(</span><span>shift</span><span>)))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>loc3</span><span>$</span><span>lat.p1</span><span> </span><span>&lt;-</span><span> </span><span>shift.vec</span><span>(</span><span>loc3</span><span>$</span><span>lat</span><span>,</span><span> </span><span>-1</span><span>)</span><span>
</span><span>loc3</span><span>$</span><span>lon.p1</span><span> </span><span>&lt;-</span><span> </span><span>shift.vec</span><span>(</span><span>loc3</span><span>$</span><span>lon</span><span>,</span><span> </span><span>-1</span><span>)</span><span>
</span><span># Calculating distances between points (in metres) with the function pointDistance from the 'raster' package.
</span><span>library</span><span>(</span><span>raster</span><span>)</span><span>
</span><span>loc3</span><span>$</span><span>dist.to.prev</span><span> </span><span>&lt;-</span><span> </span><span>apply</span><span>(</span><span>loc3</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>FUN</span><span> </span><span>=</span><span> </span><span>function</span><span>(</span><span>row</span><span>)</span><span> </span><span>{</span><span>
</span><span>pointDistance</span><span>(</span><span>c</span><span>(</span><span>as.numeric</span><span>(</span><span>as.character</span><span>(</span><span>row</span><span>[</span><span>&quot;lat.p1&quot;</span><span>])),</span><span>
</span><span>as.numeric</span><span>(</span><span>as.character</span><span>(</span><span>row</span><span>[</span><span>&quot;lon.p1&quot;</span><span>]))),</span><span>
</span><span>c</span><span>(</span><span>as.numeric</span><span>(</span><span>as.character</span><span>(</span><span>row</span><span>[</span><span>&quot;lat&quot;</span><span>])),</span><span> </span><span>as.numeric</span><span>(</span><span>as.character</span><span>(</span><span>row</span><span>[</span><span>&quot;lon&quot;</span><span>]))),</span><span>
</span><span>lonlat</span><span> </span><span>=</span><span> </span><span>T</span><span>)</span><span> </span><span># Parameter 'lonlat' has to be TRUE!
</span><span>})</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span># distance in km
</span><span>round</span><span>(</span><span>sum</span><span>(</span><span>as.numeric</span><span>(</span><span>as.character</span><span>(</span><span>loc3</span><span>$</span><span>dist.to.prev</span><span>)),</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>*</span><span>0.001</span><span>,</span><span> </span><span>digits</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 54466.08
</code></pre>
</div>
<div>
<pre><code><span>distance_p_month</span><span> </span><span>&lt;-</span><span> </span><span>aggregate</span><span>(</span><span>loc3</span><span>$</span><span>dist.to.prev</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>month_year</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>loc3</span><span>$</span><span>month_year</span><span>)),</span><span> </span><span>FUN</span><span> </span><span>=</span><span> </span><span>sum</span><span>)</span><span>
</span><span>distance_p_month</span><span>$</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>distance_p_month</span><span>$</span><span>x</span><span>*</span><span>0.001</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>ggplot</span><span>(</span><span>distance_p_month</span><span>[</span><span>-1</span><span>,</span><span> </span><span>],</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>month_year</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>x</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>month_year</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_bar</span><span>(</span><span>stat</span><span> </span><span>=</span><span> </span><span>&quot;identity&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>guides</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Distance in km&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Distance traveled per month in 2016&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;This barplot shows the sum of distances between recorded positions for 2016. In September we went to the US and Canada.&quot;</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i1.wp.com/shiring.github.io/maps/2016/12/30/Standortverlauf_post_files/figure-markdown_github/unnamed-chunk-18-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<h1>Activities</h1>
<p>Google also guesses my activity based on distance travelled per time.</p>
<p>Here again, it would take too long to look at activity from all data points.</p>
<div>
<pre><code><span>activities</span><span> </span><span>&lt;-</span><span> </span><span>loc3</span><span>$</span><span>activitys</span><span>
</span><span>list.condition</span><span> </span><span>&lt;-</span><span> </span><span>sapply</span><span>(</span><span>activities</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>!</span><span>is.null</span><span>(</span><span>x</span><span>[[</span><span>1</span><span>]]))</span><span>
</span><span>activities</span><span> </span><span>&lt;-</span><span> </span><span>activities</span><span>[</span><span>list.condition</span><span>]</span><span>
</span><span>df</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span><span> </span><span>activities</span><span>)</span><span>
</span><span>main_activity</span><span> </span><span>&lt;-</span><span> </span><span>sapply</span><span>(</span><span>df</span><span>$</span><span>activities</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>x</span><span>[[</span><span>1</span><span>]][</span><span>1</span><span>][[</span><span>1</span><span>]][</span><span>1</span><span>])</span><span>
</span><span>activities_2</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>main_activity</span><span> </span><span>=</span><span> </span><span>main_activity</span><span>,</span><span> </span><span>time</span><span> </span><span>=</span><span> </span><span>as.POSIXct</span><span>(</span><span>as.numeric</span><span>(</span><span>df</span><span>$</span><span>timestampMs</span><span>)</span><span>/</span><span>1000</span><span>,</span><span> </span><span>origin</span><span> </span><span>=</span><span> </span><span>&quot;1970-01-01&quot;</span><span>))</span><span>
</span><span>head</span><span>(</span><span>activities_2</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##   main_activity                time
## 1         still 2016-12-22 08:52:45
## 2         still 2016-12-22 08:46:57
## 3         still 2016-12-22 08:33:24
## 4         still 2016-12-22 08:21:31
## 5         still 2016-12-22 08:15:32
## 6         still 2016-12-22 08:10:25
</code></pre>
</div>
<div>
<pre><code><span>ggplot</span><span>(</span><span>activities_2</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>main_activity</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>main_activity</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>main_activity</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_bar</span><span>()</span><span> </span><span>+</span><span> </span><span>guides</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_theme</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Count&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Main activities in 2016&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Associated activity for recorded positions in 2016. Because Google records activity probabilities for each position, only the activity with highest likelihood were chosen for each position.&quot;</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
<p><img src="https://i2.wp.com/shiring.github.io/maps/2016/12/30/Standortverlauf_post_files/figure-markdown_github/unnamed-chunk-20-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p></p>
<hr>
<div>
<pre><code><span>sessionInfo</span><span>()</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## R version 3.3.2 (2016-10-31)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 7 x64 (build 7601) Service Pack 1
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] raster_2.5-8    sp_1.2-3        ggmap_2.6.2     ggplot2_2.2.0  
## [5] zoo_1.7-13      lubridate_1.6.0 jsonlite_1.1   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.8       plyr_1.8.4        bitops_1.0-6     
##  [4] tools_3.3.2       digest_0.6.10     evaluate_0.10    
##  [7] tibble_1.2        gtable_0.2.0      lattice_0.20-34  
## [10] png_0.1-7         DBI_0.5-1         mapproj_1.2-4    
## [13] yaml_2.1.14       proto_1.0.0       stringr_1.1.0    
## [16] dplyr_0.5.0       knitr_1.15.1      RgoogleMaps_1.4.1
## [19] maps_3.1.1        rprojroot_1.1     grid_3.3.2       
## [22] R6_2.2.0          jpeg_0.1-8        rmarkdown_1.2    
## [25] reshape2_1.4.2    magrittr_1.5      codetools_0.2-15 
## [28] backports_1.0.4   scales_0.4.1      htmltools_0.3.5  
## [31] assertthat_0.1    colorspace_1.3-2  geosphere_1.5-5  
## [34] labeling_0.3      stringi_1.1.2     lazyeval_0.2.0   
## [37] munsell_0.4.3     rjson_0.2.15
</code></pre>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://shiring.github.io/maps/2016/12/30/Standortverlauf_post"> Shirin's playgRound</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/35qVPwMouvM"></a></div>