Plotting GPS coordinates using ggmap

懵懂的女人 提交于 2020-01-15 06:53:28

问题


I am attempting to create a plot of gps coordinates on a map in R using ggmap. Here is the first 10 rows of the dataset I am using:

     gps[1:10,]
                   X_id       lon                timestamp      lat
    1  5555bcda65bc7d0f2c8d1a9c -4.018623 2015-05-15T09:31:06.859Z 53.23945
    2  5555bcdb65bc7d0f2c8d1a9f -4.018623 2015-05-15T09:31:07.371Z 53.23945
    3  5555bcdb65bc7d0f2c8d1aa2 -4.018623 2015-05-15T09:31:07.868Z 53.23945
    4  5555bcdc65bc7d0f2c8d1aa5 -4.018623 2015-05-15T09:31:08.364Z 53.23945
    5  5555bcdc65bc7d0f2c8d1aa8 -4.018623 2015-05-15T09:31:08.860Z 53.23945
    6  5555bcdd65bc7d0f2c8d1aab -4.018623 2015-05-15T09:31:09.372Z 53.23945
    7  5555bcdd65bc7d0f2c8d1aad -4.018623 2015-05-15T09:31:09.868Z 53.23945
    8  5555bcde65bc7d0f2c8d1ab0 -4.018623 2015-05-15T09:31:10.381Z 53.23945
    9  5555bcde65bc7d0f2c8d1ab3 -4.018623 2015-05-15T09:31:10.862Z 53.23945
    10 5555bcdf65bc7d0f2c8d1ab6 -4.018623 2015-05-15T09:31:11.373Z 53.23945

Here is what I have coded so far:

    library(ggmap)
    mapImageData <- get_googlemap(center = c(lon = median(gps$lon), lat = median(gps$lat)),zoom = 18,size = c(500, 500),maptype = c("satellite"))                              
   (ggmap(mapImageData,extent = "device") + geom_point(aes(x = lon,y = lat),data = gps,colour = "red",size = 1,pch = 20))

Which gives me the following map:

What I would like to do is create a colour gradient for the points which are related to the variable "timestamp" so that it is easier to deduce travel direction

I have tried this using:

    colour = timestamp

But the points are all black. Amy ideas?

Thanks


回答1:


You need to convert the timestamp to a numeric value first, then use scale_color_gradient to add the color...

gps <- read.table(textConnection("a X_id lon timestamp lat
                                  1  5555bcda65bc7d0f2c8d1a9c -4.018623 2015-05-15T09:31:06.859Z 53.23945
                                  2  5555bcdb65bc7d0f2c8d1a9f -4.018735 2015-05-15T09:31:07.371Z 53.23945
                                  3  5555bcdb65bc7d0f2c8d1aa2 -4.018847 2015-05-15T09:31:07.868Z 53.23945
                                  4  5555bcdc65bc7d0f2c8d1aa5 -4.018959 2015-05-15T09:31:08.364Z 53.23945
                                  5  5555bcdc65bc7d0f2c8d1aa8 -4.019061 2015-05-15T09:31:08.860Z 53.23945
                                  6  5555bcdd65bc7d0f2c8d1aab -4.019173 2015-05-15T09:31:09.372Z 53.23945
                                  7  5555bcdd65bc7d0f2c8d1aad -4.019285 2015-05-15T09:31:09.868Z 53.23945
                                  8  5555bcde65bc7d0f2c8d1ab0 -4.019397 2015-05-15T09:31:10.381Z 53.23945
                                  9  5555bcde65bc7d0f2c8d1ab3 -4.019409 2015-05-15T09:31:10.862Z 53.23945
                                  10 5555bcdf65bc7d0f2c8d1ab6 -4.019511 2015-05-15T09:31:11.373Z 53.23945"), 
                   header=T)
gps$timestamp2 <- as.POSIXct(gsub("T", " ", substring(gps$timestamp, 1, 19)))
library(ggmap)
mapImageData <- get_googlemap(center = c(lon = median(gps$lon), 
                              lat = median(gps$lat)), zoom = 18,
                              size = c(500, 500),
                              maptype = c("satellite"))                              
ggmap(mapImageData,extent = "device") + 
  geom_point(aes(x = lon,y = lat, color=as.integer(timestamp2)),
             data = gps, size = 1, pch = 20) + 
  scale_color_gradient(low="red", high="blue")



来源:https://stackoverflow.com/questions/30374505/plotting-gps-coordinates-using-ggmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!