如何绘制仅一天重叠的不同日期(基于小时:分钟)中R中的数据? [英] How to plot data in R from different days overlayed in only one day (based on hours:minutes)?

查看:58
本文介绍了如何绘制仅一天重叠的不同日期(基于小时:分钟)中R中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用R时遇到此问题:

I am having this problem with R:

我有一个名为"teste"的数据集,其中有一个日期"列(以POSIXct格式,格式为%Y-%m-%d%H:%M:%S"),每个列都有读数5个月内10分钟.

I have a dataset called "teste" that has a column for 'Date' (it's in POSIXct, format = "%Y-%m-%d %H:%M:%S"), which has readings every 10 minutes during a 5 months period.

我需要在不同的时间同时比较变量.例如,在数据集中的每个星期六绘制图形,并进行叠加.我已经有了用于设置data.frame的代码,并且只有星期六.

I need to make a comparison of the variables at the same time but in different days. For example plot every Saturday in the Dataset, overlayed. I already have the code for subsetting the data.frame and have only the Saturdays.

以下是数据示例:

  DATE                ID   VAR1   VAR2  VAR3
1 2016-09-19 00:07:47 79    19     0    OPN
2 2016-09-19 00:17:47 79    18     1    OPN
3 2016-09-19 00:27:47 79    16     3    OPN
4 2016-09-19 00:37:47 79    15     4    OPN
5 2016-09-19 00:47:47 79    16     3    OPN
6 2016-09-19 00:57:47 79    16     3    OPN

这是来自数据的输出:

structure(list(FECHA = structure(c(1474236467,1474237067,1474237667,1474841253,1474841853,1474842453),class = c("POSIXct","POSIXt"),tzone ="),ID = c(79L,79L,79L,79L,79L,79L),SLOTS = c(19L,18L,16L,14L,15L,15L),BIKES = c(0L,1L,3L,8L,7L,7L),状态=结构(c(2L,2L,2L,2L,2L,2L),.Label = c("CLS","OPN"),class ="factor")),.Names = c("FECHA","ID","SLOTS","BIKES","STATUS"),row.names = c(1L,2L,3L,1004L,1005L,1006L),class ="data.frame")

structure(list(FECHA = structure(c(1474236467, 1474237067, 1474237667, 1474841253, 1474841853, 1474842453), class = c("POSIXct", "POSIXt" ), tzone = ""), ID = c(79L, 79L, 79L, 79L, 79L, 79L), SLOTS = c(19L, 18L, 16L, 14L, 15L, 15L), BIKES = c(0L, 1L, 3L, 8L, 7L, 7L), STATUS = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("CLS", "OPN"), class = "factor")), .Names = c("FECHA", "ID", "SLOTS", "BIKES", "STATUS"), row.names = c(1L, 2L, 3L, 1004L, 1005L, 1006L ), class = "data.frame")

我尝试做:(使用的包裹lubridate)

I tried doing: (used package lubridate)

plot(as.POSIXct(paste(hour(teste$FECHA),":",minute(teste$FECHA),sep = ""), format = "%H:%M"),teste$BIKES)

它可以工作,但是使用粘贴"绝对不是实现此目的的最佳方法.可能有一种更简单,更优雅的方法,对吗?如果是,怎么办?

It works but using 'paste' is definetely not the best way for doing this. There probably is a easier and more elegant way, right? If yes, how?

如果我用type ="lines"绘制它,我会遇到一个问题,因为它不知道一天的最后一个读数不应该连接"到下一个读数的第一个读数,从而得出以下结果:(请参见从24小时到0小时跨越所有图表的线)

And I have a problem if I plot it with type = "lines", because it doesnt know that the last reading of a day shouldnt be 'connected' to the first of the next one, giving this result: (see the lines crossing all the graph from 24 hours to 0 hours)

我虽然想一次绘制一天,使用绘图然后使用线条函数,但是问题是我不知道每天有多少读数.每天应该有6 * 24 = 144个读数,但有些读数是143、142(由于获取数据时出现问题).

I though about ploting one day at a time, using the plot and then using lines functions, but the problem is that I dont know how many readings are there in each day. Each day should have 6*24=144 readings, but some have 143, 142 (because of problems while getting data).

感谢您的帮助.

推荐答案

您可以尝试

# add factor to differentiate between the different days
d$day <- factor(strftime(d$FECHA, format="%Y-%m-%d"))
# add hours, set one day
d$time <- as.POSIXct(strftime(d$FECHA, format="%H:%M:%S"), format="%H:%M:%S")
# color per day
ggplot(d, aes(x=time, y=BIKES, col=day)) + 
       geom_point() + 
       geom_line() + 
       scale_x_datetime(limits = as.POSIXct(c("2017-04-19 00:00:00", "2017-04-19 24:00:00")), date_labels = "%H:%M")
# or facet_wrap/grid
ggplot(d, aes(x=time, y=BIKES)) + geom_point() + geom_line() + 
  facet_grid(~day)+
  scale_x_datetime(limits = as.POSIXct(c("2017-04-19 00:00:00", "2017-04-19 24:00:00")), date_labels = "%H:%M")

# or use the wrap function instead of the grid function
  facet_wrap(~day, scales = "free_y")

这篇关于如何绘制仅一天重叠的不同日期(基于小时:分钟)中R中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆