Stacked bar chart with continuous time-axis as x-axis

让人想犯罪 __ 提交于 2020-01-03 03:20:14

问题


We are monitoring 3 processes A, B and C that will always either be in level X, Y, or Z. A protocol records when a process changes levels.

df = read.csv(tc <- textConnection('Time1,Process1,Level1
2013-01-09 18:00:34,A,X
2013-01-09 18:00:34,B,Y
2013-01-09 18:00:34,C,X
2013-01-09 18:00:59,A,Z
2013-01-09 18:01:06,A,X
2013-01-09 18:01:10,C,Y
2013-01-09 18:01:10,B,Z
2013-01-09 18:01:13,A,Z
2013-01-09 18:01:18,A,Off
2013-01-09 18:01:18,B,Off
2013-01-09 18:01:18,C,Off
'),header=TRUE)
close.connection(tc) 
df$Time1 = as.POSIXct(df$Time1)

Monitoring was started at 18:00:34 and switched off at 18:01:18. Between 18:00:34 and 18:00:59 process A was in level X, between 18:00:59 and 18:01:06 process A was in level Z.

We would like to show on the x-axis the continuous interval between 18:00:34 and 18:01:18, and three horizontal bars (A, B, C) of equal width that indicate the current process level at the time shown on the x-axis.

Below what we tried last. The overall structure of the chart seems right, but the Time-axis does not make much sense and data is also missing. (We don't need the Off category but it's probably easy to cut it out once the Time-axis makes sense.) Any guidance would be much appreciated.

ggplot() + 
  geom_bar(data=df, aes(x=Process1, y=Time1, fill=Level1)) + 
  coord_flip()


回答1:


I'm not sure about using geom_bar to do this, but I can get geom_line to do something similar if I increase the size of the lines.

library(ggplot2)
library(scales)
ggplot(df, aes(x=Time1, y=Process1, group=Process1, colour=Level1)) + 
  geom_line(size=5) + scale_x_datetime("",  labels = date_format("%H:%M:%S"))



来源:https://stackoverflow.com/questions/14262056/stacked-bar-chart-with-continuous-time-axis-as-x-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!