首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用ggplot2仅绘制时间

使用ggplot2仅绘制时间
EN

Stack Overflow用户
提问于 2013-01-09 00:48:07
回答 1查看 826关注 0票数 8

我有一个这样的数据框架:

代码语言:javascript
代码运行次数:0
运行
复制
 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)

代码语言:javascript
代码运行次数:0
运行
复制
dput(yy)
structure(list(Team = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "A", class = "factor"), 
Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "2012-03-06", class = "factor"), 
STime = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "07:03", class = "factor"), 
ETime = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "10:13", class = "factor")), .Names = c("Team", 
"Date", "STime", "ETime"), class = "data.frame", row.names = c(NA, 
-50L))

我喜欢看到从00:00 23:59到2小时增量的y轴,并且能够在STime值上画一条红线。

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

代码语言:javascript
代码运行次数:0
运行
复制
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中做到这一点?有没有人能给我指点/给我一个正确的方向?

致以敬意,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-06 09:10:37

你必须将你的时间格式化成实际的时间。现在它们是因子(用str(yy)检查你的数据框)。当绘制ETime时,单个时间被绘制为1并标记为“10:13”。因此,下面的解决方案首先将字符串"10:13“转换为时间(strptime),然后将其转换为POSIXct,即从原点开始的秒数(1/1/1970)。

代码语言:javascript
代码运行次数:0
运行
复制
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") )

关于geom_line to date axis的注释。

也要注意你的时区。否则,R/ggplot将根据您当地的时区进行格式化。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14219964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档