使用ggmap绘制GPS坐标 [英] Plotting GPS coordinates using ggmap
问题描述
我试图在R中使用ggmap在地图上创建gps坐标图。
这是我使用的数据集的前10行:
gps [1:10,]
$ p
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
$ b这是我到目前为止编码的内容:
library(ggmap )
mapImageData< - get_googlemap(center = c(lon = median(gps $ lon),lat = median(gps $ lat)),zoom = 18,size = c(500,500),maptype = c (卫星))
(ggmap(mapImageData,extent =device)+ geom_point(aes(x = lon,y = lat),data = gps,color =red,size = 1,pch = 20))
这给了我下面的图:
我想要做的是为与变量timestamp相关的点创建一个颜色渐变,以便更容易推断出行进方向。
我有尝试使用:
color = timestamp
但分数都是黑色的。 Amy的想法?
谢谢
解决方案您需要将时间戳然后使用
scale_color_gradient
添加颜色...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( (gps $ lon),$($ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $) b $ b 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,p ch = 20)+
scale_color_gradient(low =red,high =blue)
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
解决方案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")
这篇关于使用ggmap绘制GPS坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!