使用 ggmap 绘制 GPS 坐标
Plotting GPS coordinates using ggmap
我正在尝试使用 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))
这给了我下面的地图:
我想做的是为与变量相关的点创建颜色渐变 "timestamp" 以便更容易推断行进方向
我试过使用:
colour = timestamp
但是点都是黑色的。艾米的想法?
谢谢
您需要先将时间戳转换为数值,然后使用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")
我正在尝试使用 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))
这给了我下面的地图:
我想做的是为与变量相关的点创建颜色渐变 "timestamp" 以便更容易推断行进方向
我试过使用:
colour = timestamp
但是点都是黑色的。艾米的想法?
谢谢
您需要先将时间戳转换为数值,然后使用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")