以连续时间轴为x轴的堆积条形图 [英] Stacked bar chart with continuous time-axis as x-axis

查看:574
本文介绍了以连续时间轴为x轴的堆积条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在监控3个过程A,B和C,这些过程始终处于X,Y或Z级别。协议记录过程何时更改级别。

  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,关
2013-01-09 18:01:18,B,关
2013-01-09 18:01:18,C,关
'),header = TRUE )
close.connection(tc)
df $ Time1 = as.POSixct(df $ Time1)

监测从18:00:34开始,并在18:01:18关闭。在18:00:34和18:00:59之间,进程A在X级,18:00:59和18:01:06之间,进程A在Z级。

我们希望在x轴上显示18:00:34和18:01:18之间的连续间隔,以及三个宽度相等的水平条(A,B,C),它们表示当前过程级别时间显示在x轴上。



低于我们最后的尝试。图表的总体结构看起来不错,但时间轴没有多大意义,数据也不见了。 (我们不需要关闭类别,但是一旦时间轴有意义可能很容易将其切断)。任何指导都将非常感谢。

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

解决方案

我不确定使用 geom_bar 来做到这一点,但我可以得到 geom_line 做类似的事情,如果我增加线的大小。



<$ p $ ($)
库(尺度)
ggplot(df,aes(x = Time1,y = Process1,group = Process1,color = Level1))+
geom_line(size = 5)+ scale_x_datetime(,labels = date_format(%H:%M:%S))


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()

解决方案

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"))

这篇关于以连续时间轴为x轴的堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆