[英]Using date time data with ggplot scale_colour_gradient
I am plotting some time series GPS coordinates using ggmap and ggplot. 我正在使用ggmap和ggplot绘制一些时间序列GPS坐标。 I want to visualise the time series by creating a colour gradient. 我想通过创建颜色渐变来可视化时间序列。 I have had a couple of attempts so far as shown below. 到目前为止,我已经进行了几次尝试,如下所示。
My data can be accessed 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 ...
I have classed timestamp
as.Date
as I am aware that this can then be passed successfully into scale_colour_gradient
within the ggplot call as follows: 我已经归入timestamp
as.Date
我所知,这然后可以顺利传递到scale_colour_gradient
如下ggplot调用中:
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")
This produces the following map: 这将产生以下地图:
As you can see the colour gradient is discrete rather than the desired continuous gradient - presumably this is because it categorizes the timestamp into days? 如您所见,颜色渐变是离散的,而不是所需的连续渐变-大概是因为它将时间戳分为几天? Also The legend labels consist of 2 overlayed labels so are not clear. 另外,图例标签由2个重叠标签组成,因此不清楚。 I have tried using as.POSIXct
but this cannot be passed to trans
. 我尝试使用as.POSIXct
但是不能传递给trans
。 I have also used as.Integer
, which creates a nice colour gradient but the legend cannot be interpreted in terms of date/time. 我还使用了as.Integer
,它创建了一个不错的颜色渐变,但是无法根据日期/时间来解释图例。
Any ideas how I can get round this problem? 有什么想法可以解决这个问题吗?
Thanks 谢谢
Convert the POSIXct timestamps to integer and define the breaks and labels manually 将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")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.