R 使用ggplot2仅打印时间

R 使用ggplot2仅打印时间,r,ggplot2,R,Ggplot2,我有这样一个数据框: head(yy) Team Date STime ETime 1 A 2012-03-06 07:03 10:13 2 A 2012-03-06 07:03 10:13 3 A 2012-03-06 07:03 10:13 4 A 2012-03-06 07:03 10:13 5 A 2012-03-06 07:03 10:13 6 A 2012-03-06 07:03 10:13 dput(yy) 我希望看到y轴

我有这样一个数据框:

 head(yy)
    Team       Date STime ETime
1    A 2012-03-06 07:03 10:13
2    A 2012-03-06 07:03 10:13
3    A 2012-03-06 07:03 10:13
4    A 2012-03-06 07:03 10:13
5    A 2012-03-06 07:03 10:13
6    A 2012-03-06 07:03 10:13
dput(yy)

我希望看到y轴从00:00 23:59以2小时为增量,并且能够在时间值上画一条红线

我有这样的东西,但看起来不对:

ggplot(yy, aes(Date, ETime, group="Team")) + geom_jitter(size=0.05) + facet_wrap( ~ Team) + geom_hline(yintercept=yy$Stime, colour="red", size=2)
在ggplot2中,您将如何执行此操作?有人能给我指点方向吗


关于,

您必须将您的时间格式化为实际时间。现在它们是因素(用
str(yy)
检查数据帧)。当绘制时间时,单个时间被绘制为1并标记为“10:13”。因此,下面的解决方案首先将字符串“10:13”转换为时间(
strtime
),然后将其转换为
POSIXct
,或自原点起的秒数(1/1/1970)

库(ggplot2);图书馆(比例尺)
#将日期字符串转换为POSIXct格式

yy$TIME您能发布结果
dput(df)
(或者
dput(head(df))
如果太大,我们可以复制您的数据吗?@DavidRobinson,我刚刚放置了dput输出。您的数据没有变化。如果你只是想制作一个插图,你应该看一看,这是一个伟大的自由软件——就像R。
ggplot(yy, aes(Date, ETime, group="Team")) + geom_jitter(size=0.05) + facet_wrap( ~ Team) + geom_hline(yintercept=yy$Stime, colour="red", size=2)
library(ggplot2); library(scales)

#Convert date string into POSIXct format
yy$STime <- as.POSIXct(strptime(yy$STime, format = "%H:%M", tz = "UTC"))
yy$ETime <- as.POSIXct(strptime(yy$ETime, format = "%H:%M", tz = "UTC"))

#Define y-axis limits
lims <- as.POSIXct(strptime(c("0:00","23:59"), format = "%H:%M", tz= "UTC"))    

ggplot(yy, aes(Date, ETime, group="Team")) + geom_jitter(size=1) + facet_wrap( ~ Team) + 
  geom_hline(data = yy, aes(yintercept= as.numeric(STime)), colour="red", size=2) + 
  scale_y_datetime(limits =lims, breaks=date_breaks("2 hour"),
                   labels=date_format("%H:%M", tz = "UTC") )