简体   繁体   English

按时间创建图表

[英]Create chart by time of day

I am trying to create a graph in R, that shows how the distance to an object changes by time of day.我正在尝试在 R 中创建一个图表,显示到 object 的距离如何随一天中的时间变化。 I have a dataframe in R, that includes datetimes of GPS locations and the distance of those locations to a polygon.我在 R 中有一个 dataframe,其中包括 GPS 个位置的日期时间以及这些位置到多边形的距离。

ID                 date1       time      lat       lon    DOP     date      dist         datetime
281 Sunday, October 1, 2017 3:00:15 AM 32.59848 -90.24402 3.8 2017-10-01  57.75744 2017-10-01 03:00:15
281 Sunday, October 1, 2017 2:45:00 AM 32.59851 -90.24391 3.0 2017-10-01  54.07150 2017-10-01 02:45:00
281 Sunday, October 1, 2017 3:15:16 AM 32.59838 -90.24387 4.0 2017-10-01  68.67032 2017-10-01 03:15:16
281 Sunday, October 1, 2017 3:30:12 AM 32.59808 -90.24369 4.2 2017-10-01 103.90344 2017-10-01 03:30:12
281 Sunday, October 1, 2017 3:45:09 AM 32.59815 -90.24354 4.4 2017-10-01 100.03097 2017-10-01 03:45:09
281 Sunday, October 1, 2017 4:00:30 AM 32.59812 -90.24324 2.0 2017-10-01 114.68612 2017-10-01 04:00:30

Using this dataframe, I would like to make a graph showing the how the distance changes over time of day.使用这个 dataframe,我想制作一张图表,显示距离如何随一天中的时间变化。 This way I could see around what time of day my IDs are closer based on distance.通过这种方式,我可以根据距离查看我的 ID 在一天中的什么时间更近。 This dataset includes 4 months, from October to January.该数据集包括 4 个月,从 10 月到 1 月。 I am imagining a graph like what was created here, http://www.sthda.com/english/articles/32-r-graphics-essentials/128-plot-time-series-data-using-ggplot/ , except it uses time of day instead of year.我正在想象一个类似于此处创建的图表http://www.sthda.com/english/articles/32-r-graphics-essentials/128-plot-time-series-data-using-ggplot/ ,除了它使用一天中的时间而不是年份。

Thank you for the help.感谢您的帮助。

Using dput, here are the first 6 rows of my dataset.使用 dput,这是我的数据集的前 6 行。

structure(list(ID = c("281", "281", "281", "281", "281", "281"
), date1 = c("Sunday, October 1, 2017", "Sunday, October 1, 2017", 
"Sunday, October 1, 2017", "Sunday, October 1, 2017", "Sunday, October 1, 2017", 
"Sunday, October 1, 2017"), time = c("3:00:15 AM", "2:45:00 AM", 
"3:15:16 AM", "3:30:12 AM", "3:45:09 AM", "4:00:30 AM"), lat = c(32.59848, 
32.59851, 32.59838, 32.59808, 32.59815, 32.59812), lon = c(-90.24402, 
-90.24391, -90.24387, -90.24369, -90.24354, -90.24324), DOP = c(3.8, 
3, 4, 4.2, 4.4, 2), date = structure(c(17440, 17440, 17440, 17440, 
17440, 17440), class = "Date"), dist = c(57.7574388004, 54.0715015597, 
68.6703208583, 103.903443285, 100.030967759, 114.686118929)), row.names = c(NA, 
6L), class = "data.frame")

So your time variable is coming in as a character.所以你的时间变量是作为一个角色进来的。 To my knowledge there's no way to 1:1 extract just the time without making it a date time variable, which would not aggregate it.据我所知,如果不将时间设为日期时间变量,就无法以 1:1 的比例提取时间,这样不会聚合时间。 So you extract the individual pieces, and aggregate in the time_numeric variable.因此,您提取各个部分,并在 time_numeric 变量中聚合。

 library(tidyverse)
 data %>% 
  mutate(hours = as.numeric(str_extract(time, "^\\d+")),
         minutes = as.numeric(str_extract(time, "(?<=\\:)\\d+")),
         seconds = as.numeric(str_extract(time, "(?<=\\:\\d{2}\\:)\\d+")),
         am = ifelse(str_detect(time, "AM"), 0, 12),
         time_numeric = hours + am + minutes/60 + seconds/3600) %>% 
  ggplot(aes(time_numeric, dist)) +
  geom_line() +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 4))

So I added in some logic to round to nearest hour, the round function wrapping time_numeric will round the hour to the nearest whole number.因此,我添加了一些逻辑以舍入到最近的小时数, round time_numeric的 function 舍入会将小时舍入到最接近的整数。 I changed the geom to point so you can see it more easily.我将 geom 更改为指向,以便您可以更轻松地看到它。 If you do geom_line you need something to differentiate the lines otherwise you get a vertical line representing the range of values.如果你做 geom_line 你需要一些东西来区分这些线,否则你会得到一条代表值范围的垂直线。

library(tidyverse)
 data %>% 
  mutate(hours = as.numeric(str_extract(time, "^\\d+")),
         minutes = as.numeric(str_extract(time, "(?<=\\:)\\d+")),
         seconds = as.numeric(str_extract(time, "(?<=\\:\\d{2}\\:)\\d+")),
         am = ifelse(str_detect(time, "AM"), 0, 12),
         time_numeric = round(hours + am + minutes/60 + seconds/3600)) %>% 
  ggplot(aes(time_numeric, dist)) +
  geom_point() +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 4))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM