Reputation: 15
Hey everyone I have been trying to plot some data where x-axis would be times of a certain day and the y-axis would be numeric values of Free Flow Speed of a certain road segment. A simple example of a portion of my data is shown below
> mydf1
# A tibble: 11 x 3
Date_UTC Time Free_Flow_Speed
<date> <chr> <dbl>
1 2019-05-07 08:15:00 81.0
2 2019-05-07 08:30:00 9.36
3 2019-05-07 08:45:00 4.15
4 2019-05-07 09:00:00 7.18
5 2019-05-07 09:15:00 9.78
6 2019-05-07 09:30:00 16.4
7 2019-05-07 09:45:00 97.9
8 2019-05-07 10:00:00 101.
9 2019-05-07 10:15:00 101.
10 2019-05-07 10:30:00 102.
11 2019-05-07 10:45:00 100.
I have created the "Time" by converting the "Date_UTC" using
mydf1$Time <- format(as.POSIXct(data2$`UTC Date and Time`), format = "%H:%M:%S")
I tried to plot "Time" in x-axis and "Free_Flow_Speed" in y-axis:
> plot(mydf1$Time, mydf1$Free_Flow_Speed)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
I tried to investigate around and apparently, the time is being converted into only "NAs", maybe because "Time" is "Char" and "Free_Flow_Speed" is "double/numeric"
> str(mydf1)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 11 obs. of 3 variables:
$ Date_UTC : Date, format: "2019-05-07" "2019-05-07" "2019-05-07" "2019-05-07" ...
$ Time : chr "08:15:00" "08:30:00" "08:45:00" "09:00:00" ...
$ Free_Flow_Speed: num 80.99 9.36 4.15 7.18 9.78 ...
I tried to convert "Free_Flow_Speed" to "Char" but with no avail.
>mydf1 <- as.numeric(as.character(mydf1$Free_Flow_Speed))
Error in mydf1$Free_Flow_Speed : $ operator is invalid for atomic vectors
> mydf2 <- c(mydf1$Time, as.numeric(as.character(mydf1$Free_Flow_Speed)))
dput(data)
> dput(mydf1)
structure(list(Date_UTC = structure(c(18023, 18023, 18023, 18023,
18023, 18023, 18023, 18023, 18023, 18023, 18023), class = "Date"),
Time = c("08:15:00", "08:30:00", "08:45:00", "09:00:00",
"09:15:00", "09:30:00", "09:45:00", "10:00:00", "10:15:00",
"10:30:00", "10:45:00"), Free_Flow_Speed = c(80.99, 9.36,
4.15, 7.18, 9.78, 16.37, 97.91, 100.54, 100.81, 102.46, 100.27
)), row.names = c(NA, -11L), class = c("tbl_df", "tbl", "data.frame"
))
I would love if someone could shed some light on how to plot with "Times". In excel this is doable, but I'm trying to explore ggplot2 with this dataset and would love to be able to realize what is it that I'm doing wrong.
Thanks in advance!
Upvotes: 1
Views: 1195
Reputation: 1116
Your line
mydf1$Time <- format(as.POSIXct(data2$`UTC Date and Time`), format = "%H:%M:%S")
especially
data2$`UTC Date and Time`
do not correspond to the input data you provide.
A proper conversion can be achieved by first pasting days and hours together and then converting :
df1 <- df1 %>% mutate(all_date=as.POSIXct(paste(Date_UTC,Time),format = "%Y-%m-%d %H:%M:%S"))
Finally just plot it:
plot(df1$all_date,df1$Free_Flow_Speed)
Upvotes: 1