Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot group(按日期和打印时间)在x轴上从同一日期时间开始_R_Datetime_Ggplot2_Posixct - Fatal编程技术网

R ggplot group(按日期和打印时间)在x轴上从同一日期时间开始

R ggplot group(按日期和打印时间)在x轴上从同一日期时间开始,r,datetime,ggplot2,posixct,R,Datetime,Ggplot2,Posixct,我有一个带整数变量的df,counts,还有一个POSIXctdatetime变量。datetime变量表示不同日期的分钟间隔: counts datetime 246 02/11/2011 19:00 237 02/11/2011 19:01 268 02/11/2011 19:02 496 02/11/2011 19:03 259 02/11/2011 19:04 548 09/12/2011 20:14 我想有一个每天的线图,在x轴上有不同的HH:MM。我可以通过lubridate获

我有一个带整数变量的
df
counts
,还有一个POSIXct
datetime
变量。datetime变量表示不同日期的分钟间隔:

counts  datetime
246 02/11/2011 19:00
237 02/11/2011 19:01
268 02/11/2011 19:02
496 02/11/2011 19:03
259 02/11/2011 19:04

548 09/12/2011 20:14
我想有一个每天的线图,在x轴上有不同的HH:MM。我可以通过
lubridate
获得日期、小时和分钟:

require(lubridate)
day(df$datetime)
minute(df$datetime)
但我无法将它们组合在一起(我尝试了将datetime转换为HH:MM字符的方法,但没有成功)。我缺少下面的“x=因子(HH:MM的日期时间)”。有什么意见吗

ggplot(df, aes(x=factor(HH:MM of datetime), y=counts, group=day(datetime)) + geom_line()
示例:每一行应该是不同的一天。x轴应为HH:MM,y轴为计数。

数据:

你可以试试

library(ggplot2)
library(scales)
df$date <- factor(as.Date(df$datetime))
df$dtime <- as.POSIXct(sprintf('%s %s:%s', Sys.Date(), 
                       format(df$datetime, '%H:%M'), '00'))

p <- ggplot(df, aes(x=dtime, y=all, group=date, color=date))+
                                           geom_line()

p+
  scale_x_datetime(breaks=date_breaks('1 hour'), 
                            labels=date_format('%H:%M')) +
                               theme_light()
库(ggplot2)
图书馆(比例尺)

看到你们的样本数据和问题,我很困惑。您是否打算每天创建一个图形?或者你打算在一个图形中有多天?也许你需要
df$datetime每天都是一条线,这样绘图将有许多天(许多线)。所有的日子都有相同的计数时间(从19:00到21:00)。@akrun,当我尝试时,
df$datetime@user3507584在这种情况下,只需删除第一步
df$time
library(ggplot2)
library(scales)
df$date <- factor(as.Date(df$datetime))
df$dtime <- as.POSIXct(sprintf('%s %s:%s', Sys.Date(), 
                       format(df$datetime, '%H:%M'), '00'))

p <- ggplot(df, aes(x=dtime, y=all, group=date, color=date))+
                                           geom_line()

p+
  scale_x_datetime(breaks=date_breaks('1 hour'), 
                            labels=date_format('%H:%M')) +
                               theme_light()