我正在使用ggmap和ggplot绘制一些时间序列GPS坐标。我想通过创建颜色渐变来可视化时间序列。到目前为止,我已经进行了几次尝试。
我的数据可以访问here
Dec7 = read.csv("7-12-15.csv", header = TRUE, stringsAsFactors = FALSE)
Dec7$timestamp <- as.Date(Dec7$timestamp)
head(Dec7)
X_id seq_id timestamp lon address lat rssi sensor gps_quality batt_v
1 56656ecd0dd8e408d8c2e43f 71 2015-12-07 -3.780899 208 53.20252 -63 gps 1 3274
2 56656ed20dd8e408d8c2e440 72 2015-12-07 -3.780958 208 53.20246 -63 gps 1 3274
3 56656edc0dd8e408d8c2e441 73 2015-12-07 -3.780967 208 53.20246 -65 gps 1 3274
4 56656ee60dd8e408d8c2e442 74 2015-12-07 -3.780968 208 53.20242 -64 gps 1 3274
5 56656ef10dd8e408d8c2e443 75 2015-12-07 -3.780997 208 53.20240 -64 gps 1 3274
6 56656efa0dd8e408d8c2e446 76 2015-12-07 -3.780965 208 53.20243 -64 gps 1 3274
str(Dec7)
data.frame': 22420 obs. of 10 variables:
$ X_id : chr "56656ecd0dd8e408d8c2e43f" "56656ed20dd8e408d8c2e440" "56656edc0dd8e408d8c2e441" "56656ee60dd8e408d8c2e442" ...
$ seq_id : int 71 72 73 74 75 76 77 78 86 87 ...
$ timestamp : Date, format: "2015-12-07" "2015-12-07" "2015-12-07" "2015-12-07" ...
$ lon : num -3.78 -3.78 -3.78 -3.78 -3.78 ...
$ address : num 208 208 208 208 208 208 208 208 208 208 ...
$ lat : num 53.2 53.2 53.2 53.2 53.2 ...
$ rssi : int -63 -63 -65 -64 -64 -64 -64 -63 -64 -64 ...
$ sensor : chr "gps" "gps" "gps" "gps" ...
$ gps_quality: int 1 1 1 1 1 1 1 1 1 1 ...
$ batt_v : int 3274 3274 3274 3274 3274 3274 3274 3274 3274 3274 ...
我已将timestamp
as.Date
归为一类,因为我知道这可以在ggplot调用中成功传递到scale_colour_gradient
,如下所示:
mapImageData <- get_googlemap(center = c(lon = median(Dec7$lon),
lat = median(Dec7$lat)), zoom = 17,
size = c(500, 500),
maptype = c("satellite"))
sheep_hiraetlyn_Dec7_map = ggmap(mapImageData,extent = "device") +
geom_point(aes(x = lon,y = lat, color=timestamp),
data = Dec7, size = 1, pch = 20) +
scale_color_gradient(trans = "date", low="red", high="blue")
这会产生以下地图:
正如您所看到的,颜色渐变是离散的而不是所需的连续渐变 - 可能这是因为它将时间戳分为几天?
此外,图例标签由2个重叠标签组成,因此不清晰。
我尝试过使用as.POSIXct
,但无法将其传递给trans
。
我还使用了as.Integer
,它创建了一个漂亮的颜色渐变,但图例不能用日期/时间来解释。
我有什么想法可以解决这个问题吗?
谢谢
答案 0 :(得分:2)
将POSIXct时间戳转换为整数并手动定义中断和标签
Dec7$time <- as.integer(Dec7$timestamp)
labels <- pretty(Dec7$timestamp, 5)
ggmap(mapImageData,extent = "device") +
geom_point(
aes(x = lon,y = lat, color=time),
data = Dec7, size = 1, pch = 20
) +
scale_color_gradient(
low="red", high="blue",
breaks = as.integer(labels),
labels = format(labels, "%m/%d %H:%M")
)