以R为单位显示开始、结束和持续时间的时间序列

以R为单位显示开始、结束和持续时间的时间序列,r,ggplot2,data-visualization,time-series,candlestick-chart,R,Ggplot2,Data Visualization,Time Series,Candlestick Chart,我有以下数据: > Data Date Start End 1 2011-11-15 12:01:27 12:30:15 2 2011-11-16 12:01:25 12:32:15 3 2011-11-17 12:01:02 12:39:12 4 2011-11-19 12:01:12 12:30:18 我还添加了一个持续时间列 Data[,4] <- as.numeric(difftime(Data$End,Data$

我有以下数据:

> Data
          Date    Start       End
1   2011-11-15 12:01:27 12:30:15 
2   2011-11-16 12:01:25 12:32:15 
3   2011-11-17 12:01:02 12:39:12 
4   2011-11-19 12:01:12 12:30:18
我还添加了一个持续时间列

Data[,4] <- as.numeric(difftime(Data$End,Data$Start))
names(Data)[4] <- "Duration"

Data[,4]我不使用ggplot,但我可以给你一个基本的R解决方案

# Generate the data
date <- c("2011-11-15", "2011-11-16", "2011-11-17", "2011-11-19")
start <- c("12:01:27", "12:01:25", "12:01:02", "12:01:12")
end <- c("12:30:15", "12:32:15", "12:39:12", "12:30:18")

# Put everything in a data frame and convert to POSIXct objects
# The times will be all converted to today's date
# but this will not influence the plot
df <- data.frame(date = as.POSIXct(date),
                 start = as.POSIXct(start, format="%H:%M:%S"), 
                 end = as.POSIXct(end, format="%H:%M:%S"))

# Get the working range for the axes in order to make them nicer (see below)
x.from <- as.POSIXct(min(date))
x.to <- as.POSIXct(max(date))
y.from <- as.POSIXct(min(start), format="%H:%M:%S")
y.to <- as.POSIXct(max(end), format="%H:%M:%S")

# Create an empty plot, as rect will not create a new one
# We put no axes on the plot
plot(0, "n", xaxt="n", yaxt="n", ylab="", xlab="Day", 
     ylim=c(from, to), xlim=range(df$date))

# Now draw the rectangles (I made them 2 hours-wide)
rect(df$date-3600, df$start, df$date+3600, df$end, col="black")

days <- seq(x.from, x.to, 24*3600)
times <- seq(y.from, y.to, 300) # 5 min (=300 s) axis ticks
# Finally add the axes
axis(1, at=days, labels=strftime(days, "%d/%m"))
axis(2, at=times, labels=strftime(times, "%H:%M"), las=1)
#生成数据

date我使用了与nico类似的结构(谢谢!):

接下来,我们将其放在一个包含矩形角的数据框中:

##I've made the rectangles 2 hours wide
df = data.frame(date = as.POSIXct(date),
         ystart = as.POSIXct(start, format="%H:%M:%S"), 
         yend = as.POSIXct(end, format="%H:%M:%S"),
         xstart=as.POSIXct(paste(date, "12:00:00"), format="%Y-%m-%d %H:%M:%S"),
         xend = as.POSIXct(paste(date, "14:00:00"), format="%Y-%m-%d %H:%M:%S"))
然后我们只使用
geom\u rect

ggplot() + geom_rect(data=df, aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart))
如果要根据条件将其中一些列设置为红色,只需在数据框上创建一个附加列:

##Your condition is something to do with the sd
df$isRed = c(TRUE, FALSE)
然后添加两个ggplot图层:

ggplot() + geom_rect(data=subset(df, !isRed), aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart)) +
           geom_rect(data=subset(df, isRed), aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart), colour="red")
示例图


抱歉,现在没有时间添加颜色位,今晚将尝试添加它(除非出现其他更优雅的解决方案),这非常接近。我正在寻找更宽的条形图(几乎可以触摸到),但一旦我添加了2年左右的数据点,这肯定是一个有趣的图表。谢谢你的指导@Mittenchops:只需在
rect
调用中将3600更改为更高的值,以获得更宽的条;)你太棒了,@nico,谢谢。我需要更好地理解这里的机制(以及我将如何改变它以处理一个扩展多年的时间序列,并改变极端情况的颜色),但这帮助我制作了一个奇妙的图表。谢谢@Mittenchops:R以秒为单位“思考”,因此x轴上的每个刻度为1天,如果您的条形图宽度为7200秒,则意味着它们将覆盖2小时,因此2个刻度之间的空间为1/12。这是否更容易仍有争议,但您可以指定
颜色
参数作为美学参数,并手动调整比例,如:
ggplot(df,aes(日期,ymin=y.from,ymax=y.to,color=isRed))+geom_linerange()+scale_color_manual(值=c(“真”表示“红”,“假”表示“黑”),guide表示“无”)
。使用两种颜色,添加单独的几何图形同样容易,但可能没有可缩放性。很抱歉让人困惑。尼科从美学角度得到了我想要的东西。这很接近,我喜欢ggplot,但几何图形linerange能产生比线条更宽的线条吗?
ggplot() + geom_rect(data=subset(df, !isRed), aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart)) +
           geom_rect(data=subset(df, isRed), aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart), colour="red")