GPS data visualization

gganimate, ggmap

As an employee, I am trying to construct my portfolio these days. By sharing my research materials, I hope that my article makes you either become interested in me or the driving telematics topic itself! =]

I wrote all the code in this article to use them as a tool for my Ph.D. research. Thus, they are not efficient code and have much room for improvement, and also, it might have some typo or error. If you found any typo or error in my code, feel free to leave a comment or contact me. I would be delighted and appreciate your feedback.

This article uses the following R packages.

library(gganimate)
library(ggmap)
library(dplyr)
library(magrittr)
library(tibble)
library(knitr)
library(kableExtra)

GPS data visualization

GPS is probably the most popular data format, which has information about the position of the device. At the beginning of my research, I tried to use it to record my test route. To record the telematics data, I attached my phone to my vehicle and recorded the position of my vehicle per second using a smartphone sensor app.

Here is the sample sensor data that contains the information of my commute route data.

mytelematics <- read.csv("https://raw.githubusercontent.com/issactoast/theissacleedotcom/master/static/data/sampletrip.csv", header = TRUE, fill = TRUE)

Selecting GPS data from raw telematics data

kable(head(mytelematics, 100), format = "html") %>% 
  kable_styling() %>% 
  scroll_box(width = "100%", height = "300px")
Timestamp accelX.g. accelY.g. accelZ.g. accelUserX.g. accelUserY.g. accelUserZ.g. gyroX.rad.s. gyroY.rad.s. gyroZ.rad.s. Roll.rads. Pitch.rads. Yaw.rads. Lat Long Speed.m.s. TrueHeading Alt.m. HorizontalAccuracy.m. VerticalAccuracy.m. ActivityType ActivityConfidence Pressure.kilopascals. RelativeAltitude.meters. magX.쨉T. magY.쨉T. magZ.쨉T. calMagX calMagY calMagZ
24-May-2019 18:11:23.018 -0.0005 0.0169 -1.0066 -0.0106 -0.0119 -0.0177 -0.0705 0.0773 0.0009 0.0024 -0.0239 -2.2836 41.66253 -91.53799 0 88.1709 197.2922 5 4 Automotive High 98.6373 -10.2496 18.4652 20.2407 -279.8046 0 0 0
24-May-2019 18:11:23.056 -0.0072 0.0160 -1.0361 -0.0056 -0.0064 0.0031 -0.0054 0.0350 -0.0128 0.0023 -0.0233 -2.2835 41.66253 -91.53799 0 88.1709 197.2922 5 4 Automotive High 98.6373 -10.2496 16.6897 21.3060 -278.4545 0 0 0
24-May-2019 18:11:23.097 -0.0039 0.0139 -1.0067 -0.0121 -0.0148 -0.0193 -0.0246 0.0382 -0.0043 0.0024 -0.0231 -2.2834 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 15.6244 18.8203 -280.1421 0 0 0
24-May-2019 18:11:23.138 -0.0088 0.0123 -1.0182 -0.0045 -0.0037 -0.0165 -0.0182 0.0265 -0.0139 0.0025 -0.0229 -2.2833 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 16.8672 20.7733 -278.6232 0 0 0
24-May-2019 18:11:23.171 -0.0064 0.0195 -1.0093 -0.0050 -0.0073 -0.0105 -0.0150 0.0350 -0.0086 0.0021 -0.0224 -2.2832 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 16.6897 19.8856 -278.7920 0 0 0
24-May-2019 18:11:23.212 -0.0032 0.0130 -1.0071 0.0092 -0.0030 -0.0087 -0.0171 0.0286 -0.0107 0.0024 -0.0220 -2.2832 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 16.3346 20.5958 -278.7920 0 0 0
24-May-2019 18:11:23.258 -0.0079 0.0174 -1.0158 0.0004 -0.0027 -0.0112 -0.0171 0.0360 -0.0075 0.0022 -0.0216 -2.2831 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 17.0448 19.1754 -279.8046 0 0 0
24-May-2019 18:11:23.290 -0.0077 0.0168 -1.0086 0.0019 -0.0008 -0.0169 -0.0118 0.0276 -0.0128 0.0024 -0.0214 -2.2831 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 18.1101 21.3060 -279.1295 0 0 0
24-May-2019 18:11:23.328 -0.0092 0.0147 -1.0209 0.0046 -0.0035 -0.0188 -0.0182 0.0350 -0.0075 0.0026 -0.0208 -2.2830 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 15.4468 20.7733 -278.9608 0 0 0
24-May-2019 18:11:23.366 -0.0022 0.0157 -1.0122 0.0029 -0.0055 -0.0155 -0.0128 0.0265 -0.0107 0.0025 -0.0202 -2.2829 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 17.0448 19.8856 -278.4545 0 0 0
24-May-2019 18:11:23.405 -0.0007 0.0136 -1.0143 0.0050 -0.0050 -0.0124 -0.0160 0.0318 -0.0086 0.0022 -0.0199 -2.2828 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 17.2223 21.1284 -280.3109 0 0 0
24-May-2019 18:11:23.449 -0.0022 0.0175 -1.0140 0.0067 -0.0051 -0.0134 -0.0150 0.0297 -0.0107 0.0023 -0.0195 -2.2827 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 17.9325 20.7733 -280.6484 0 0 0
24-May-2019 18:11:23.485 -0.0022 0.0166 -1.0149 0.0097 -0.0031 -0.0159 -0.0171 0.0329 -0.0096 0.0025 -0.0193 -2.2826 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 17.3999 20.5958 -278.7920 0 0 0
24-May-2019 18:11:23.530 0.0025 0.0196 -1.0171 0.0070 -0.0011 -0.0131 -0.0150 0.0286 -0.0118 0.0024 -0.0191 -2.2826 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 17.0448 19.1754 -279.4670 0 0 0
24-May-2019 18:11:23.558 0.0030 0.0206 -1.0143 0.0087 0.0005 -0.0185 -0.0160 0.0318 -0.0086 0.0026 -0.0191 -2.2825 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 17.0448 19.1754 -279.4670 0 0 0
24-May-2019 18:11:23.598 0.0051 0.0173 -1.0198 0.0072 0.0002 -0.0167 -0.0160 0.0297 -0.0118 0.0025 -0.0189 -2.2825 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 17.0448 20.9509 -280.4796 0 0 0
24-May-2019 18:11:23.636 0.0081 0.0175 -1.0157 0.0033 -0.0020 -0.0176 -0.0150 0.0329 -0.0096 0.0025 -0.0187 -2.2824 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 16.6897 21.6611 -279.4670 0 0 0
24-May-2019 18:11:23.679 0.0096 0.0168 -1.0150 0.0064 -0.0020 -0.0188 -0.0150 0.0297 -0.0096 0.0024 -0.0185 -2.2823 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 16.8672 18.9978 -279.9733 0 0 0
24-May-2019 18:11:23.720 0.0114 0.0167 -1.0136 0.0067 -0.0034 -0.0138 -0.0171 0.0318 -0.0096 0.0026 -0.0182 -2.2822 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6373 -10.2496 16.3346 19.8856 -279.4670 0 0 0
24-May-2019 18:11:23.756 0.0093 0.0141 -1.0184 0.0048 0.0007 -0.0121 -0.0171 0.0329 -0.0096 0.0023 -0.0180 -2.2821 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6371 -10.2310 17.2223 20.0631 -278.6232 0 0 0
24-May-2019 18:11:23.792 0.0112 0.0163 -1.0177 -0.0087 -0.0051 -0.0142 -0.0182 0.0318 -0.0086 0.0023 -0.0180 -2.2821 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6371 -10.2310 17.7550 20.2407 -279.1295 0 0 0
24-May-2019 18:11:23.831 0.0098 0.0150 -1.0159 -0.0010 -0.0009 -0.0150 -0.0171 0.0318 -0.0107 0.0023 -0.0177 -2.2820 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6371 -10.2310 17.7550 19.5305 -279.1295 0 0 0
24-May-2019 18:11:23.869 0.0074 0.0175 -1.0190 -0.0037 -0.0004 -0.0145 -0.0150 0.0329 -0.0096 0.0025 -0.0175 -2.2819 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6371 -10.2310 17.0448 19.1754 -279.1295 0 0 0
24-May-2019 18:11:23.908 0.0119 0.0189 -1.0152 -0.0026 -0.0018 -0.0142 -0.0150 0.0329 -0.0096 0.0022 -0.0173 -2.2819 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6371 -10.2310 18.2876 18.9978 -280.6484 0 0 0
24-May-2019 18:11:23.949 0.0113 0.0207 -1.0142 -0.0049 -0.0015 -0.0125 -0.0150 0.0318 -0.0096 0.0022 -0.0173 -2.2817 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6371 -10.2310 17.2223 17.9325 -279.2983 0 0 0
24-May-2019 18:11:23.985 0.0078 0.0163 -1.0181 -0.0068 -0.0015 -0.0109 -0.0160 0.0297 -0.0118 0.0022 -0.0173 -2.2817 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6371 -10.2310 16.6897 20.2407 -279.8046 0 0 0
24-May-2019 18:11:24.027 0.0066 0.0151 -1.0149 -0.0093 -0.0018 -0.0134 -0.0160 0.0308 -0.0107 0.0025 -0.0171 -2.2816 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6371 -10.2310 16.8672 18.6427 -278.9608 0 0 0
24-May-2019 18:11:24.062 0.0058 0.0148 -1.0160 -0.0018 -0.0012 -0.0166 -0.0160 0.0308 -0.0107 0.0027 -0.0171 -2.2815 41.66253 -91.53799 0 88.1709 197.2821 5 4 Automotive High 98.6371 -10.2310 16.1570 20.0631 -278.9608 0 0 0
24-May-2019 18:11:24.111 0.0036 0.0135 -1.0127 -0.0091 -0.0018 -0.0119 -0.0150 0.0318 -0.0118 0.0028 -0.0169 -2.2814 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 17.0448 19.1754 -279.4670 0 0 0
24-May-2019 18:11:24.140 0.0014 0.0149 -1.0116 -0.0102 -0.0042 -0.0143 -0.0150 0.0318 -0.0118 0.0025 -0.0167 -2.2814 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 17.5774 20.4182 -280.3109 0 0 0
24-May-2019 18:11:24.178 -0.0011 0.0173 -1.0133 -0.0074 -0.0010 -0.0109 -0.0160 0.0318 -0.0096 0.0025 -0.0165 -2.2813 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 17.3999 19.5305 -279.4670 0 0 0
24-May-2019 18:11:24.219 -0.0053 0.0163 -1.0146 -0.0049 0.0018 -0.0154 -0.0160 0.0329 -0.0096 0.0026 -0.0165 -2.2812 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.3346 19.5305 -279.1295 0 0 0
24-May-2019 18:11:24.256 -0.0077 0.0159 -1.0122 -0.0037 0.0036 -0.0156 -0.0139 0.0329 -0.0118 0.0027 -0.0166 -2.2811 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 17.7550 21.3060 -279.4670 0 0 0
24-May-2019 18:11:24.296 -0.0082 0.0129 -1.0144 -0.0021 0.0039 -0.0156 -0.0160 0.0329 -0.0107 0.0027 -0.0165 -2.2811 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 17.3999 19.1754 -279.8046 0 0 0
24-May-2019 18:11:24.326 -0.0086 0.0138 -1.0112 -0.0015 0.0029 -0.0180 -0.0171 0.0329 -0.0107 0.0029 -0.0165 -2.2810 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.6897 19.1754 -279.1295 0 0 0
24-May-2019 18:11:24.366 -0.0082 0.0158 -1.0096 0.0064 -0.0006 -0.0141 -0.0139 0.0318 -0.0107 0.0028 -0.0163 -2.2809 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.6897 19.1754 -279.1295 0 0 0
24-May-2019 18:11:24.404 -0.0096 0.0168 -1.0117 0.0037 0.0022 -0.0170 -0.0150 0.0318 -0.0096 0.0029 -0.0162 -2.2809 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 18.1101 18.4652 -280.1421 0 0 0
24-May-2019 18:11:24.444 -0.0029 0.0187 -1.0156 0.0046 -0.0011 -0.0150 -0.0150 0.0339 -0.0107 0.0026 -0.0162 -2.2808 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 18.1101 19.1754 -276.7669 0 0 0
24-May-2019 18:11:24.483 -0.0017 0.0182 -1.0166 0.0059 0.0007 -0.0147 -0.0150 0.0329 -0.0107 0.0026 -0.0161 -2.2807 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 17.9325 20.4182 -278.2857 0 0 0
24-May-2019 18:11:24.521 -0.0044 0.0180 -1.0154 0.0075 0.0012 -0.0183 -0.0160 0.0318 -0.0096 0.0024 -0.0161 -2.2806 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 17.2223 19.7080 -279.2983 0 0 0
24-May-2019 18:11:24.562 0.0027 0.0174 -1.0174 0.0082 0.0000 -0.0180 -0.0160 0.0339 -0.0107 0.0023 -0.0161 -2.2806 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.6897 20.2407 -281.1547 0 0 0
24-May-2019 18:11:24.596 0.0052 0.0182 -1.0171 0.0088 0.0004 -0.0160 -0.0150 0.0297 -0.0118 0.0023 -0.0161 -2.2805 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 18.1101 20.2407 -279.1295 0 0 0
24-May-2019 18:11:24.639 0.0058 0.0211 -1.0140 0.0097 -0.0023 -0.0179 -0.0139 0.0318 -0.0096 0.0023 -0.0161 -2.2804 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 17.5774 19.3529 -279.2983 0 0 0
24-May-2019 18:11:24.682 0.0047 0.0165 -1.0184 0.0091 0.0018 -0.0132 -0.0160 0.0318 -0.0107 0.0023 -0.0161 -2.2803 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.3346 20.5958 -279.8046 0 0 0
24-May-2019 18:11:24.715 0.0074 0.0158 -1.0169 0.0070 0.0009 -0.0141 -0.0139 0.0339 -0.0118 0.0023 -0.0161 -2.2803 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.5121 20.0631 -279.6358 0 0 0
24-May-2019 18:11:24.753 0.0085 0.0163 -1.0166 -0.0095 -0.0017 -0.0111 -0.0171 0.0329 -0.0118 0.0022 -0.0160 -2.2802 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 15.8019 18.9978 -278.9608 0 0 0
24-May-2019 18:11:24.802 0.0103 0.0153 -1.0174 0.0036 -0.0004 -0.0158 -0.0150 0.0329 -0.0096 0.0021 -0.0161 -2.2801 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 18.4652 18.4652 -279.1295 0 0 0
24-May-2019 18:11:24.830 0.0105 0.0150 -1.0157 -0.0084 0.0003 -0.0121 -0.0150 0.0339 -0.0118 0.0021 -0.0160 -2.2801 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 18.4652 18.8203 -278.4545 0 0 0
24-May-2019 18:11:24.870 0.0134 0.0172 -1.0157 -0.0014 -0.0012 -0.0163 -0.0139 0.0329 -0.0118 0.0019 -0.0160 -2.2800 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.8672 20.4182 -277.6107 0 0 0
24-May-2019 18:11:24.909 0.0122 0.0179 -1.0120 -0.0018 0.0002 -0.0132 -0.0171 0.0318 -0.0118 0.0021 -0.0160 -2.2800 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.3346 20.5958 -279.8046 0 0 0
24-May-2019 18:11:24.947 0.0074 0.0171 -1.0147 -0.0028 0.0000 -0.0109 -0.0160 0.0339 -0.0107 0.0022 -0.0160 -2.2799 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.6897 18.1101 -279.1295 0 0 0
24-May-2019 18:11:24.985 0.0087 0.0177 -1.0134 -0.0063 -0.0014 -0.0160 -0.0139 0.0308 -0.0086 0.0023 -0.0160 -2.2798 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.1570 20.0631 -278.9608 0 0 0
24-May-2019 18:11:25.024 0.0059 0.0160 -1.0151 -0.0031 0.0017 -0.0158 -0.0139 0.0308 -0.0086 0.0023 -0.0161 -2.2797 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 16.3346 19.8856 -279.1295 0 0 0
24-May-2019 18:11:25.066 0.0026 0.0151 -1.0165 -0.0123 0.0000 -0.0113 -0.0160 0.0329 -0.0107 0.0023 -0.0160 -2.2796 41.66253 -91.53799 0 88.1709 197.2597 5 4 Automotive High 98.6371 -10.2310 17.5774 20.7733 -279.9733 0 0 0
24-May-2019 18:11:25.104 0.0016 0.0139 -1.0147 -0.0099 0.0011 -0.0140 -0.0160 0.0318 -0.0107 0.0023 -0.0160 -2.2795 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 17.7550 19.1754 -279.1295 0 0 0
24-May-2019 18:11:25.140 -0.0006 0.0167 -1.0108 -0.0081 -0.0016 -0.0131 -0.0160 0.0318 -0.0107 0.0023 -0.0159 -2.2794 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 17.2223 19.7080 -278.2857 0 0 0
24-May-2019 18:11:25.179 -0.0049 0.0165 -1.0132 -0.0031 0.0018 -0.0132 -0.0171 0.0308 -0.0107 0.0023 -0.0159 -2.2794 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 18.1101 19.5305 -279.1295 0 0 0
24-May-2019 18:11:25.217 -0.0059 0.0162 -1.0148 -0.0031 0.0051 -0.0128 -0.0160 0.0308 -0.0096 0.0024 -0.0160 -2.2793 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 18.2876 19.3529 -279.6358 0 0 0
24-May-2019 18:11:25.256 -0.0058 0.0146 -1.0138 -0.0020 0.0025 -0.0177 -0.0139 0.0318 -0.0096 0.0024 -0.0160 -2.2792 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 18.8203 20.2407 -279.4670 0 0 0
24-May-2019 18:11:25.299 -0.0055 0.0155 -1.0150 -0.0078 -0.0005 -0.0151 -0.0160 0.0308 -0.0107 0.0023 -0.0160 -2.2791 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 16.3346 19.5305 -279.1295 0 0 0
24-May-2019 18:11:25.335 -0.0060 0.0162 -1.0119 0.0058 0.0013 -0.0189 -0.0150 0.0318 -0.0128 0.0025 -0.0160 -2.2791 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 16.8672 20.7733 -278.9608 0 0 0
24-May-2019 18:11:25.376 -0.0031 0.0177 -1.0134 0.0039 0.0005 -0.0188 -0.0150 0.0318 -0.0096 0.0025 -0.0159 -2.2790 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 18.8203 20.5958 -279.4670 0 0 0
24-May-2019 18:11:25.413 -0.0004 0.0212 -1.0150 0.0064 -0.0009 -0.0142 -0.0160 0.0308 -0.0107 0.0025 -0.0159 -2.2789 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 18.8203 20.5958 -279.4670 0 0 0
24-May-2019 18:11:25.461 -0.0014 0.0177 -1.0155 0.0096 -0.0007 -0.0183 -0.0150 0.0265 -0.0118 0.0025 -0.0159 -2.2788 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 16.5121 19.3529 -278.2857 0 0 0
24-May-2019 18:11:25.491 -0.0007 0.0159 -1.0192 0.0061 0.0000 -0.0166 -0.0150 0.0318 -0.0118 0.0026 -0.0159 -2.2788 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 17.2223 19.7080 -279.2983 0 0 0
24-May-2019 18:11:25.532 0.0046 0.0188 -1.0155 0.0060 0.0005 -0.0179 -0.0160 0.0318 -0.0107 0.0024 -0.0159 -2.2787 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 17.0448 20.2407 -279.4670 0 0 0
24-May-2019 18:11:25.571 0.0083 0.0169 -1.0153 0.0058 0.0010 -0.0154 -0.0150 0.0276 -0.0107 0.0025 -0.0159 -2.2786 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 17.9325 20.0631 -279.2983 0 0 0
24-May-2019 18:11:25.607 0.0071 0.0158 -1.0186 0.0084 0.0015 -0.0105 -0.0150 0.0286 -0.0118 0.0025 -0.0160 -2.2786 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 17.5774 18.9978 -279.2983 0 0 0
24-May-2019 18:11:25.645 0.0099 0.0175 -1.0177 0.0062 -0.0007 -0.0129 -0.0150 0.0297 -0.0128 0.0025 -0.0160 -2.2785 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 17.7550 19.1754 -280.8171 0 0 0
24-May-2019 18:11:25.684 0.0123 0.0157 -1.0186 -0.0083 -0.0008 -0.0173 -0.0160 0.0286 -0.0107 0.0024 -0.0160 -2.2785 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 17.0448 19.5305 -278.7920 0 0 0
24-May-2019 18:11:25.722 0.0103 0.0176 -1.0198 0.0020 -0.0008 -0.0112 -0.0160 0.0286 -0.0096 0.0025 -0.0160 -2.2784 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 18.1101 19.8856 -279.4670 0 0 0
24-May-2019 18:11:25.765 0.0111 0.0150 -1.0173 -0.0023 -0.0021 -0.0137 -0.0150 0.0276 -0.0096 0.0025 -0.0160 -2.2783 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2310 17.2223 19.7080 -278.2857 0 0 0
24-May-2019 18:11:25.800 0.0098 0.0155 -1.0133 -0.0059 -0.0023 -0.0147 -0.0160 0.0265 -0.0107 0.0023 -0.0159 -2.2782 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2363 16.6897 19.1754 -279.4670 0 0 0
24-May-2019 18:11:25.839 0.0089 0.0172 -1.0122 -0.0051 -0.0010 -0.0135 -0.0139 0.0276 -0.0107 0.0024 -0.0159 -2.2782 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2363 17.9325 19.7080 -279.2983 0 0 0
24-May-2019 18:11:25.877 0.0039 0.0177 -1.0148 -0.0079 0.0013 -0.0122 -0.0160 0.0286 -0.0096 0.0023 -0.0159 -2.2781 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2363 17.5774 19.7080 -278.6232 0 0 0
24-May-2019 18:11:25.916 -0.0007 0.0180 -1.0141 -0.0090 0.0008 -0.0141 -0.0160 0.0297 -0.0096 0.0022 -0.0159 -2.2780 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2363 17.2223 19.7080 -277.6107 0 0 0
24-May-2019 18:11:25.957 -0.0013 0.0168 -1.0110 -0.0056 0.0011 -0.0168 -0.0139 0.0286 -0.0107 0.0021 -0.0160 -2.2779 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2363 17.0448 19.8856 -277.7794 0 0 0
24-May-2019 18:11:25.996 -0.0043 0.0157 -1.0147 -0.0039 0.0019 -0.0151 -0.0160 0.0297 -0.0107 0.0023 -0.0161 -2.2778 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2363 18.1101 18.8203 -279.4670 0 0 0
24-May-2019 18:11:26.034 -0.0040 0.0149 -1.0123 -0.0069 0.0001 -0.0190 -0.0150 0.0276 -0.0118 0.0021 -0.0161 -2.2778 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2363 17.5774 20.4182 -279.6358 0 0 0
24-May-2019 18:11:26.070 -0.0038 0.0140 -1.0135 0.0063 0.0000 -0.0173 -0.0139 0.0297 -0.0107 0.0024 -0.0161 -2.2777 41.66253 -91.53799 0 88.1709 197.3375 5 4 Automotive High 98.6371 -10.2363 15.8019 21.1284 -278.9608 0 0 0
24-May-2019 18:11:26.110 -0.0050 0.0170 -1.0114 0.0045 0.0004 -0.0156 -0.0139 0.0318 -0.0118 0.0026 -0.0159 -2.2776 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 17.5774 19.7080 -279.6358 0 0 0
24-May-2019 18:11:26.149 -0.0059 0.0164 -1.0147 0.0099 -0.0020 -0.0182 -0.0150 0.0276 -0.0107 0.0027 -0.0159 -2.2776 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 18.8203 18.8203 -279.1295 0 0 0
24-May-2019 18:11:26.186 -0.0031 0.0144 -1.0171 0.0037 0.0006 -0.0153 -0.0150 0.0276 -0.0107 0.0030 -0.0159 -2.2775 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 18.1101 20.5958 -279.8046 0 0 0
24-May-2019 18:11:26.231 -0.0037 0.0159 -1.0165 0.0028 -0.0004 -0.0166 -0.0150 0.0276 -0.0107 0.0029 -0.0157 -2.2774 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 17.3999 20.2407 -278.4545 0 0 0
24-May-2019 18:11:26.266 -0.0003 0.0176 -1.0185 0.0052 0.0004 -0.0185 -0.0160 0.0276 -0.0118 0.0029 -0.0157 -2.2773 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 18.8203 20.5958 -278.4545 0 0 0
24-May-2019 18:11:26.305 0.0007 0.0186 -1.0172 0.0044 0.0018 -0.0159 -0.0160 0.0276 -0.0118 0.0028 -0.0159 -2.2773 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 16.5121 20.0631 -279.6358 0 0 0
24-May-2019 18:11:26.343 0.0059 0.0173 -1.0161 -0.0019 0.0034 -0.0159 -0.0128 0.0265 -0.0107 0.0025 -0.0159 -2.2772 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 16.1570 20.0631 -277.9482 0 0 0
24-May-2019 18:11:26.388 0.0052 0.0187 -1.0138 0.0092 0.0006 -0.0138 -0.0150 0.0286 -0.0107 0.0025 -0.0160 -2.2771 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 17.9325 18.9978 -279.6358 0 0 0
24-May-2019 18:11:26.422 0.0065 0.0170 -1.0172 0.0075 0.0004 -0.0134 -0.0139 0.0276 -0.0096 0.0027 -0.0160 -2.2770 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 18.2876 19.7080 -280.9859 0 0 0
24-May-2019 18:11:26.460 0.0113 0.0162 -1.0163 -0.0053 -0.0011 -0.0113 -0.0139 0.0276 -0.0096 0.0025 -0.0160 -2.2769 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 18.2876 19.7080 -280.9859 0 0 0
24-May-2019 18:11:26.499 0.0126 0.0130 -1.0180 0.0056 0.0006 -0.0146 -0.0150 0.0308 -0.0107 0.0025 -0.0160 -2.2769 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 15.9795 19.8856 -280.1421 0 0 0
24-May-2019 18:11:26.537 0.0105 0.0168 -1.0145 0.0012 -0.0006 -0.0147 -0.0160 0.0297 -0.0107 0.0025 -0.0159 -2.2768 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 17.3999 20.9509 -279.4670 0 0 0
24-May-2019 18:11:26.576 0.0097 0.0150 -1.0175 -0.0032 0.0000 -0.0127 -0.0150 0.0286 -0.0107 0.0024 -0.0159 -2.2767 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 17.0448 20.5958 -280.1421 0 0 0
24-May-2019 18:11:26.619 0.0105 0.0198 -1.0153 -0.0054 -0.0013 -0.0144 -0.0160 0.0286 -0.0107 0.0023 -0.0158 -2.2767 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 15.8019 21.1284 -278.6232 0 0 0
24-May-2019 18:11:26.653 0.0086 0.0180 -1.0154 -0.0062 0.0004 -0.0122 -0.0160 0.0308 -0.0096 0.0023 -0.0159 -2.2766 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 16.6897 20.2407 -279.8046 0 0 0
24-May-2019 18:11:26.692 0.0036 0.0175 -1.0172 -0.0098 0.0007 -0.0119 -0.0160 0.0297 -0.0107 0.0022 -0.0160 -2.2765 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 17.7550 20.5958 -278.4545 0 0 0
24-May-2019 18:11:26.732 0.0038 0.0153 -1.0137 -0.0002 0.0020 -0.0184 -0.0171 0.0318 -0.0096 0.0024 -0.0160 -2.2765 41.66253 -91.53799 0 88.1709 197.2889 5 4 Automotive High 98.6371 -10.2363 17.0448 19.8856 -278.4545 0 0 0
24-May-2019 18:11:26.781 0.0005 0.0153 -1.0121 -0.0084 0.0011 -0.0132 -0.0160 0.0297 -0.0096 0.0023 -0.0160 -2.2764 41.66253 -91.53799 0 87.1665 197.2889 5 4 Automotive High 98.6371 -10.2363 16.5121 20.0631 -278.6232 0 0 0
24-May-2019 18:11:26.809 -0.0028 0.0163 -1.0140 -0.0079 -0.0008 -0.0104 -0.0150 0.0297 -0.0096 0.0022 -0.0160 -2.2763 41.66253 -91.53799 0 87.1665 197.2889 5 4 Automotive High 98.6371 -10.2363 16.8672 19.7080 -278.9608 0 0 0
24-May-2019 18:11:26.847 -0.0035 0.0160 -1.0165 -0.0059 0.0010 -0.0126 -0.0171 0.0308 -0.0086 0.0022 -0.0159 -2.2763 41.66253 -91.53799 0 87.1665 197.2889 5 4 Automotive High 98.6371 -10.2363 17.0448 22.0162 -279.8046 0 0 0

Even though there are many variables related to other sensors and accuracy of GPS, let us choose only four variables as follows;

gpsdata <- mytelematics %>% select(Timestamp, Long, Lat, Speed.m.s.)
colnames(gpsdata) <- c("Timestamp", "x", "y", "speed")

# convert speed unit (from m/s to mph)
gpsdata$speed <- gpsdata$speed * 2.23694 

Thus, the GPS data we are working on in this article consists of time, longitude, latitude, and speed information. Note that I changed the unit of the speed from m/s to mph.

kable(head(gpsdata, 100), format = "html") %>% 
  kable_styling() %>% 
  scroll_box(width = "100%", height = "300px")
Timestamp x y speed
24-May-2019 18:11:23.018 -91.53799 41.66253 0
24-May-2019 18:11:23.056 -91.53799 41.66253 0
24-May-2019 18:11:23.097 -91.53799 41.66253 0
24-May-2019 18:11:23.138 -91.53799 41.66253 0
24-May-2019 18:11:23.171 -91.53799 41.66253 0
24-May-2019 18:11:23.212 -91.53799 41.66253 0
24-May-2019 18:11:23.258 -91.53799 41.66253 0
24-May-2019 18:11:23.290 -91.53799 41.66253 0
24-May-2019 18:11:23.328 -91.53799 41.66253 0
24-May-2019 18:11:23.366 -91.53799 41.66253 0
24-May-2019 18:11:23.405 -91.53799 41.66253 0
24-May-2019 18:11:23.449 -91.53799 41.66253 0
24-May-2019 18:11:23.485 -91.53799 41.66253 0
24-May-2019 18:11:23.530 -91.53799 41.66253 0
24-May-2019 18:11:23.558 -91.53799 41.66253 0
24-May-2019 18:11:23.598 -91.53799 41.66253 0
24-May-2019 18:11:23.636 -91.53799 41.66253 0
24-May-2019 18:11:23.679 -91.53799 41.66253 0
24-May-2019 18:11:23.720 -91.53799 41.66253 0
24-May-2019 18:11:23.756 -91.53799 41.66253 0
24-May-2019 18:11:23.792 -91.53799 41.66253 0
24-May-2019 18:11:23.831 -91.53799 41.66253 0
24-May-2019 18:11:23.869 -91.53799 41.66253 0
24-May-2019 18:11:23.908 -91.53799 41.66253 0
24-May-2019 18:11:23.949 -91.53799 41.66253 0
24-May-2019 18:11:23.985 -91.53799 41.66253 0
24-May-2019 18:11:24.027 -91.53799 41.66253 0
24-May-2019 18:11:24.062 -91.53799 41.66253 0
24-May-2019 18:11:24.111 -91.53799 41.66253 0
24-May-2019 18:11:24.140 -91.53799 41.66253 0
24-May-2019 18:11:24.178 -91.53799 41.66253 0
24-May-2019 18:11:24.219 -91.53799 41.66253 0
24-May-2019 18:11:24.256 -91.53799 41.66253 0
24-May-2019 18:11:24.296 -91.53799 41.66253 0
24-May-2019 18:11:24.326 -91.53799 41.66253 0
24-May-2019 18:11:24.366 -91.53799 41.66253 0
24-May-2019 18:11:24.404 -91.53799 41.66253 0
24-May-2019 18:11:24.444 -91.53799 41.66253 0
24-May-2019 18:11:24.483 -91.53799 41.66253 0
24-May-2019 18:11:24.521 -91.53799 41.66253 0
24-May-2019 18:11:24.562 -91.53799 41.66253 0
24-May-2019 18:11:24.596 -91.53799 41.66253 0
24-May-2019 18:11:24.639 -91.53799 41.66253 0
24-May-2019 18:11:24.682 -91.53799 41.66253 0
24-May-2019 18:11:24.715 -91.53799 41.66253 0
24-May-2019 18:11:24.753 -91.53799 41.66253 0
24-May-2019 18:11:24.802 -91.53799 41.66253 0
24-May-2019 18:11:24.830 -91.53799 41.66253 0
24-May-2019 18:11:24.870 -91.53799 41.66253 0
24-May-2019 18:11:24.909 -91.53799 41.66253 0
24-May-2019 18:11:24.947 -91.53799 41.66253 0
24-May-2019 18:11:24.985 -91.53799 41.66253 0
24-May-2019 18:11:25.024 -91.53799 41.66253 0
24-May-2019 18:11:25.066 -91.53799 41.66253 0
24-May-2019 18:11:25.104 -91.53799 41.66253 0
24-May-2019 18:11:25.140 -91.53799 41.66253 0
24-May-2019 18:11:25.179 -91.53799 41.66253 0
24-May-2019 18:11:25.217 -91.53799 41.66253 0
24-May-2019 18:11:25.256 -91.53799 41.66253 0
24-May-2019 18:11:25.299 -91.53799 41.66253 0
24-May-2019 18:11:25.335 -91.53799 41.66253 0
24-May-2019 18:11:25.376 -91.53799 41.66253 0
24-May-2019 18:11:25.413 -91.53799 41.66253 0
24-May-2019 18:11:25.461 -91.53799 41.66253 0
24-May-2019 18:11:25.491 -91.53799 41.66253 0
24-May-2019 18:11:25.532 -91.53799 41.66253 0
24-May-2019 18:11:25.571 -91.53799 41.66253 0
24-May-2019 18:11:25.607 -91.53799 41.66253 0
24-May-2019 18:11:25.645 -91.53799 41.66253 0
24-May-2019 18:11:25.684 -91.53799 41.66253 0
24-May-2019 18:11:25.722 -91.53799 41.66253 0
24-May-2019 18:11:25.765 -91.53799 41.66253 0
24-May-2019 18:11:25.800 -91.53799 41.66253 0
24-May-2019 18:11:25.839 -91.53799 41.66253 0
24-May-2019 18:11:25.877 -91.53799 41.66253 0
24-May-2019 18:11:25.916 -91.53799 41.66253 0
24-May-2019 18:11:25.957 -91.53799 41.66253 0
24-May-2019 18:11:25.996 -91.53799 41.66253 0
24-May-2019 18:11:26.034 -91.53799 41.66253 0
24-May-2019 18:11:26.070 -91.53799 41.66253 0
24-May-2019 18:11:26.110 -91.53799 41.66253 0
24-May-2019 18:11:26.149 -91.53799 41.66253 0
24-May-2019 18:11:26.186 -91.53799 41.66253 0
24-May-2019 18:11:26.231 -91.53799 41.66253 0
24-May-2019 18:11:26.266 -91.53799 41.66253 0
24-May-2019 18:11:26.305 -91.53799 41.66253 0
24-May-2019 18:11:26.343 -91.53799 41.66253 0
24-May-2019 18:11:26.388 -91.53799 41.66253 0
24-May-2019 18:11:26.422 -91.53799 41.66253 0
24-May-2019 18:11:26.460 -91.53799 41.66253 0
24-May-2019 18:11:26.499 -91.53799 41.66253 0
24-May-2019 18:11:26.537 -91.53799 41.66253 0
24-May-2019 18:11:26.576 -91.53799 41.66253 0
24-May-2019 18:11:26.619 -91.53799 41.66253 0
24-May-2019 18:11:26.653 -91.53799 41.66253 0
24-May-2019 18:11:26.692 -91.53799 41.66253 0
24-May-2019 18:11:26.732 -91.53799 41.66253 0
24-May-2019 18:11:26.781 -91.53799 41.66253 0
24-May-2019 18:11:26.809 -91.53799 41.66253 0
24-May-2019 18:11:26.847 -91.53799 41.66253 0

Converting Time variable

As we can see in the above table, the Timestamp variable contains the information of the time, but it is string data. Since we want to use the data for the animated plot, we need to convert them into the relative time scale, whose starting value should be zero seconds.

My solution is a little bit inefficient and possibly has an error if the recording time is around midnight (if this is the case then we need to think about the date too), but since my driving data mostly during the afternoon, I converted the time string into the data with unit second as follows;

# seperate date and time
temp_time <- as.character(gpsdata$Timestamp) %>% strsplit(" ") %>% unlist

# select the time information and split into three part
temp_time <- temp_time[seq(2, length(temp_time), by = 2)] %>% strsplit(":")

# conversion formula for hh:mm:ss to seconds. 
convert_time <- function(timelist){
    timelist <- unlist(timelist)
    hr <- as.numeric(timelist[1]) * 3600
    min <- as.numeric(timelist[2]) * 60
    sec <- as.numeric(timelist[3])
    result <- hr + min + sec
    result
}

# apply convert_time function to temp_time
gpsdata$Timestamp <- lapply(temp_time, convert_time) %>% unlist()
gpsdata$Timestamp <- gpsdata$Timestamp - gpsdata$Timestamp[1]

Now, the timestamp of the GPS data starts with zero as follows;

kable(head(gpsdata, 50), format = "html") %>% 
  kable_styling() %>% 
  scroll_box(width = "100%", height = "300px")
Timestamp x y speed
0.000 -91.53799 41.66253 0
0.038 -91.53799 41.66253 0
0.079 -91.53799 41.66253 0
0.120 -91.53799 41.66253 0
0.153 -91.53799 41.66253 0
0.194 -91.53799 41.66253 0
0.240 -91.53799 41.66253 0
0.272 -91.53799 41.66253 0
0.310 -91.53799 41.66253 0
0.348 -91.53799 41.66253 0
0.387 -91.53799 41.66253 0
0.431 -91.53799 41.66253 0
0.467 -91.53799 41.66253 0
0.512 -91.53799 41.66253 0
0.540 -91.53799 41.66253 0
0.580 -91.53799 41.66253 0
0.618 -91.53799 41.66253 0
0.661 -91.53799 41.66253 0
0.702 -91.53799 41.66253 0
0.738 -91.53799 41.66253 0
0.774 -91.53799 41.66253 0
0.813 -91.53799 41.66253 0
0.851 -91.53799 41.66253 0
0.890 -91.53799 41.66253 0
0.931 -91.53799 41.66253 0
0.967 -91.53799 41.66253 0
1.009 -91.53799 41.66253 0
1.044 -91.53799 41.66253 0
1.093 -91.53799 41.66253 0
1.122 -91.53799 41.66253 0
1.160 -91.53799 41.66253 0
1.201 -91.53799 41.66253 0
1.238 -91.53799 41.66253 0
1.278 -91.53799 41.66253 0
1.308 -91.53799 41.66253 0
1.348 -91.53799 41.66253 0
1.386 -91.53799 41.66253 0
1.426 -91.53799 41.66253 0
1.465 -91.53799 41.66253 0
1.503 -91.53799 41.66253 0
1.544 -91.53799 41.66253 0
1.578 -91.53799 41.66253 0
1.621 -91.53799 41.66253 0
1.664 -91.53799 41.66253 0
1.697 -91.53799 41.66253 0
1.735 -91.53799 41.66253 0
1.784 -91.53799 41.66253 0
1.812 -91.53799 41.66253 0
1.852 -91.53799 41.66253 0
1.891 -91.53799 41.66253 0

Frequency of GPS data

One thing you may notice is that the frequency of our timestamp is higher than 1 Hz, which means the gap between each time point is less than 1 second. Even though GPS data has been recorded as 1 Hz, it is recorded with higher frequency, like 25 Hz in this case, since other recorded sensors use a higher frequency recording rate, so GPS data is also recorded at the same rate. However, the actual information about the position of a vehicle is changed every 1 second, which can be confirmed by drawing the time vs. speed graph as follows;

p <- ggplot(data = gpsdata, aes(x = Timestamp, y = speed)) +
        geom_line(alpha = 0.3) +
        labs(x = "time (sec)", y = "speed (mph)",
             title = "Trip speed") +
        theme_bw()
p

Thus, since we do not need redundant information, let us select the data for every 25 data points.

gpsdata <- gpsdata[seq(1, length(gpsdata$Timestamp), by = 25),]

Thus, the final data set for the GPS visualization is as follows;

Timestamp x y speed
0.000 -91.53799 41.66253 0.000000
0.967 -91.53799 41.66253 0.000000
1.929 -91.53799 41.66253 0.000000
2.898 -91.53799 41.66253 0.000000
3.869 -91.53799 41.66253 0.000000
4.841 -91.53799 41.66253 0.000000
5.805 -91.53799 41.66253 0.000000
6.772 -91.53799 41.66253 0.000000
7.746 -91.53799 41.66253 0.000000
8.714 -91.53798 41.66253 1.342164
9.678 -91.53796 41.66254 6.442387
10.646 -91.53791 41.66254 10.088599
11.613 -91.53784 41.66254 13.645334
12.581 -91.53776 41.66254 16.195446
13.552 -91.53766 41.66253 17.962628
14.523 -91.53756 41.66254 18.835035
15.481 -91.53746 41.66254 19.506117
16.449 -91.53735 41.66254 19.819288
17.413 -91.53724 41.66254 20.490370
18.384 -91.53713 41.66254 21.564102
19.353 -91.53701 41.66254 22.704941
20.320 -91.53689 41.66254 22.951004
21.287 -91.53677 41.66254 23.487870
22.253 -91.53664 41.66254 23.644456
23.231 -91.53651 41.66254 23.756303
24.193 -91.53639 41.66254 23.331284
25.161 -91.53639 41.66254 23.331284
26.136 -91.53626 41.66254 23.197068
27.099 -91.53614 41.66253 23.554978
28.067 -91.53601 41.66253 24.919512
29.037 -91.53587 41.66253 26.395892
29.997 -91.53572 41.66253 27.223560
30.964 -91.53557 41.66252 27.648578
31.933 -91.53543 41.66252 27.245929
32.901 -91.53530 41.66252 24.405015
33.870 -91.53517 41.66251 21.273299
34.837 -91.53506 41.66251 19.662703
35.810 -91.53497 41.66251 17.291546
36.780 -91.53489 41.66251 13.511118
37.746 -91.53483 41.66251 8.008245
38.708 -91.53481 41.66251 2.863283
39.678 -91.53481 41.66251 0.000000
40.646 -91.53481 41.66251 0.000000
41.615 -91.53481 41.66251 0.000000
42.587 -91.53481 41.66251 0.000000
43.556 -91.53481 41.66251 0.000000
44.516 -91.53481 41.66251 0.000000
45.484 -91.53481 41.66251 0.000000
46.455 -91.53481 41.66251 0.000000
47.423 -91.53481 41.66251 0.000000

GPS data visualization with ggmap and gganimate

I received the API key from the Google Map registration page to access the google map via ggmap package. Once you obtained your API key, register the key as follows;

# api from google map
api <- "your API code here"
register_google(key = api)

Let’s decide the size of the background. Since the get_googlemap function in ggmap package only provide the zoom parameter, we need to calculate the center of the whole trip.

center_vec <- colMeans(gpsdata[,2:3])
map_obj <- get_googlemap(center = as.numeric(center_vec),
                       maptype = "hybrid",
                       zoom = 17)
ggmap(map_obj)

Once you made a map object as in the above code, you can display the map using ggmap function. Now, let us put the GPS data on the map. We can think ggmap(map_obj) as a ggplot object so that we can add the layer on it.

p <- ggmap(map_obj) + 
  geom_path(data = gpsdata,
            aes(x = x, y = y),
            size = 0.7, color = "yellow") +
  geom_point(data = gpsdata,
            aes(x, y), col = "red", size = 3)
p

Let us remove the labels for x and y axes so that we can see a plain map.

p <- p + 
  labs(title = "Telematics data tracking", x = "", y = "") +
  guides(fill=FALSE) +
  theme(axis.text = element_blank(),
        axis.title = element_text(size = 15, face="bold"),
        plot.margin = margin(0.2, 0.2, 0.2, 0.2, "cm"),
        axis.ticks.x=element_blank(),
        axis.ticks.y=element_blank())
p

gganimate

Now finally, we are ready to use gganimate to make a cool plot. transition_reveal function reveals the path and the data points as the timestamp variable increase. For the smooth movement of the dot, I use the ease_aes function. The R code will save the result as

p <- p + transition_reveal(Timestamp) +
    ease_aes('linear')
p

Hope you enjoy the reading! I am always open to your feedback to improve my code.

Avatar
Issac Lee
PhD candidate, ABD

I believe anyone can learn anything with a proper education process.

comments powered by Disqus