我正在尝试使用ggmap在R中的地图上创建gps坐标图。 这是我正在使用的数据集的前10行:
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
以下是我目前编码的内容:
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))
这给了我以下地图:
我想要做的是为与变量&#34;时间戳&#34;相关的点创建颜色渐变。这样可以更容易地推断出旅行方向
我试过这个:
colour = timestamp
但是这些都是黑色的。艾米的想法?
由于
答案 0 :(得分:2)
您需要先将时间戳转换为数值,然后使用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("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")