DateTime limits of scale_color_gradient not workin

2019-09-06 06:53发布

问题:

I want to generate multiple maps over a base map (TMap). Basically, the same map, but categorized by week.

Okay, here's the base code:

library(ggplot2)
library(ggmap)
library(lubridate)

map=TMap + geom_point(aes(x=Lon, y=Lat, size=count, color=as.Date(y$`2016-35`$DateTime)), data=y$`2016-35`, alpha=1) +
scale_colour_gradient(limits=as.Date(c(min(y$`2016-35`$DateTime), max(y$`2016-35`$DateTime))),low="navy", high="orange", name="Date", trans="date")
ggsave(map,path=dir,filename = paste("l","test",".png", sep=""))

y= Is the list of dataframes I'm working with.

'2016-35' = This is just one week's of data, i.e. one of the dataframes in the y list.

Each of the data frames looks like this:

Tower Tag_ID       HourID count    week       Lon      Lat           DateTime   day
T01     53 2016-38_3_22    95 2016-38 -94.44542 42.81252 2016-09-21 22:00:00   Wed
T01     53  2016-38_4_1    95 2016-38 -94.44542 42.81252 2016-09-22 01:00:00 Thurs
T01     53 2016-35_7_22    82 2016-35 -94.44542 42.81252 2016-08-28 22:00:00   Sun
T01     53 2016-35_7_21   177 2016-35 -94.44542 42.81252 2016-08-28 21:00:00   Sun
T01     53  2016-35_6_1   143 2016-35 -94.44542 42.81252 2016-09-03 01:00:00   Sat


> str()
'data.frame':   671 obs. of  9 variables:
$ Tower   : chr  "T01" "T01" "T01" "T01" ...
$ Tag_ID  : int  53 53 53 53 53 53 53 53 53 53 ...
$ HourID  : chr  "2016-38_3_22" "2016-38_4_1" "2016-35_7_22" "2016-35_7_21" ...
$ count   : int  95 95 82 177 143 103 75 85 107 162 ...
$ week    : chr  "2016-38" "2016-38" "2016-35" "2016-35" ...
$ Lon     : num  -94.4 -94.4 -94.4 -94.4 -94.4 ...
$ Lat     : num  42.8 42.8 42.8 42.8 42.8 ...
$ DateTime: POSIXct, format: "2016-09-21 22:00:00" "2016-09-22 01:00:00" ...
$ day     : chr  "Wed" "Thurs" "Sun" "Sun" ...

When I do them one week at a time, this code spits out exactly what I want:

B-e-a-utiful. However, when I try and put it in a for loop, it all goes to sh*t.

dir=choose.dir()
nm <- names(y)
for (i in 1:length(nm)) {
    map=TMap + geom_point(aes(x=Lon, y=Lat, size=count, color=as.Date(y$nm[i]$DateTime)), data=y$nm[i], alpha=1) +
    scale_colour_gradient(limits=as.Date(c(min(y$nm[i]$DateTime), max(y$nm[i]$DateTime))),low="navy", high="orange", name="Date", trans="date")
    ggsave(map,path=dir,filename = paste("l",nm[i],".png", sep=""))
 }

I get the following error message: Error in as.Date.numeric(c(min(y$nm[i]$DateTime), max(y$nm[i]$DateTime))) : 'origin' must be supplied

The only thing that I've changed swapped '2016-35' with nm[i]. I've tried shifting around the syntax a bunch of times. I get a variety of error messages, but it's mostly the one above. When I supply the origin, it gives me the following error no matter what format I put it in.

Error in charToDate(x) : character string is not in a standard unambiguous format

Any clues as to why this is only getting wonky in the For loop are greatly appreciated. Do I need to structure the loop differently so it reads the data? Should I use:

for i in seq_along(nm)    #???

I'm lost!

回答1:

After a bit more research, I figured this out. I had to restructure it a bit, but I got something that works. I'm parsing out each data frame before running the mapping code using print().

for (name in names(y)) {
    q= print(y[[name]])
    map=TMap + geom_point(aes(x=Lon, y=Lat, size=count, color=as.Date(DateTime)), data=q, alpha=1) +
    scale_colour_gradient(limits=as.Date(c(min(q$DateTime), max(q$DateTime)), origin = "1970-01-01"),low="navy", high="orange", name="Date", trans="date")
    ggsave(map,path=dir,filename = paste("l",print(name),".png", sep=""))
 }

By adding the extra step of isolating the dataframe first, it uncomplicated my code and got it to run smoothly.

Good luck!