我试图为我的数据绘制时间间隔。基本上我想比较不同个人的2个系统的录音。代码可能有点低效。
require(reshape2)
require(ggplot2)
df.system1.person1<-data.frame(
time.start.person1=c("12:12:30","12:13:10","12:13:50"),
time.end.person1=c("12:12:35","12:13:20","12:13:55")
)
df.system2.person1=data.frame(
time.start.person1=c("12:12:30","12:13:50"),
time.end.person1=c("12:13:25","12:14:00")
)
df.system1.person2=data.frame(
time.start.person2=c("12:12:30","12:13:10","12:13:50"),
time.end.person2=c("12:12:35","12:13:20","12:13:55")
)
df.system2.person2=data.frame(
time.start.person2=c("12:12:30","12:13:50"),
time.end.person2=c("12:13:25","12:14:00")
)
ndf.system1.person1 <- melt(df.system1.person1, measure.vars = c("time.start.person1", "time.end.person1"))
ndf.system2.person1 <- melt(df.system2.person1, measure.vars = c("time.start.person1", "time.end.person1"))
ndf.system1.person2 <- melt(df.system1.person2, measure.vars = c("time.start.person2", "time.end.person2"))
ndf.system2.person2 <- melt(df.system2.person2, measure.vars = c("time.start.person2", "time.end.person2"))
ndf.system1.person1$value2<-as.POSIXct(strptime(ndf.system1.person1$value, "%H:%M:%S"))
ndf.system2.person1$value2<-as.POSIXct(strptime(ndf.system2.person1$value, "%H:%M:%S"))
ndf.system1.person2$value2<-as.POSIXct(strptime(ndf.system1.person2$value, "%H:%M:%S"))
ndf.system2.person2$value2<-as.POSIXct(strptime(ndf.system2.person2$value, "%H:%M:%S"))
data=rbind(ndf.system1.person1,ndf.system2.person1,ndf.system1.person2,ndf.system2.person2)
data$Arg[1:6]="System1"
data$Arg[7:10]="System2"
data$Arg[11:16]="System1"
data$Arg[17:20]="System2"
data$Ind[1:10]="Person 1"
data$Ind[11:20]="Person 2"
ggplot(data,aes(x=value2,y=Arg))+geom_line(size=1)+ facet_grid(.~Ind,scales="free_x",space="free_x")+xlab("") + ylab("") +theme_bw()+theme(legend.position="none",axis.text=element_text(size=14),panel.grid.major = element_blank(),axis.title.y = element_text(vjust=+1),panel.grid.minor = element_blank(), axis.title=element_text(size=14))
然而,我得到一条实线而不是时间间隔。时间间隔应为start.time-end.time。另外,在x轴上我想得到HH:MM格式。谢谢。
答案 0 :(得分:0)
您合并数据的方式似乎不适合您想要制作的地图。您可以在下面找到一种不同的方法来组合您最初创建的数据。然后,您可以使用geom_linerange
制作您的情节。
df <- do.call(rbind, lapply(list(cbind(df.system1.person1, Arg = "System1", Ind = "Person 1"),
cbind(df.system2.person1, Arg = "System2", Ind = "Person 1"),
cbind(df.system1.person2, Arg = "System1", Ind = "Person 2"),
cbind(df.system2.person2, Arg = "System2", Ind = "Person 2")),
'colnames<-', c('start.time', 'end.time', 'Arg', 'Ind')))
#
ggplot(df, aes(x = Arg, ymin = start.time, ymax=end.time)) +
geom_linerange(size = 1) +
coord_flip() +
facet_grid(.~Ind,scales="free_y",space="free_y") +
xlab("") + ylab("") +
theme_bw() +
theme(axis.text=element_text(size=14),
axis.text.x = element_text(angle = 20, vjust=0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
编辑:要更改时间格式,您需要先将时间转换为POSIXct
格式,然后再使用scale_y_datetime
。
# change formats
df$start.time <- as.POSIXct(df$start.time, format='%H:%M:%S')
df$end.time <- as.POSIXct(df$end.time, format='%H:%M:%S')
# load additional package
require(scales)
# added scale_y_datetime.
# The example data you provide span less than 2 minutes,
# so I used the date format with seconds and 30 second breaks
# You need to change this to get 15 minute breaks and no seconds.
ggplot(df, aes(x = Arg, ymin = start.time, ymax=end.time)) +
geom_linerange(size = 1) +
scale_y_datetime(breaks = date_breaks("30 secs"), # change to "15 mins"
labels = date_format("%H:%M:%S")) + # change to "%H:%M"
coord_flip() +
facet_grid(.~Ind,scales="free_y",space="free_y") +
xlab("") + ylab("") +
theme_bw() +
theme(axis.text=element_text(size=14),
axis.text.x = element_text(angle = 20, vjust=0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())