Reputation: 851
I am plotting some time series GPS coordinates using ggmap and ggplot. 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:
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.
I have tried using as.POSIXct
but this cannot be passed to trans
.
I have also used as.Integer
, which creates a nice colour gradient but the legend cannot be interpreted in terms of date/time.
Any ideas how I can get round this problem?
Thanks
Upvotes: 3
Views: 2975
Reputation: 18487
Convert the POSIXct timestamps to integer and define the breaks and labels manually
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")
)
Upvotes: 3