如何使用geom_bar stat =“identity”设置Bin宽度在时间系列情节? [英] How to set Bin Width With geom_bar stat="identity" in a time Series plot?

查看:1518
本文介绍了如何使用geom_bar stat =“identity”设置Bin宽度在时间系列情节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个使用条形图的时间序列,并将Bin宽度设置为0.9。但我似乎无法做到这一点。我已经四处搜寻,但到目前为止找不到任何有用的信息。这是一个限制,如果stat =身份?$ / b>

以下是一个示例数据和图表。
干杯!

 时间<  -  c('2015-06-08 00:59:00','2015-06-08 02:48:00','2015-06 -08 06:43:00','2015-06-08 08:59:00','2015-06-08 10:59:00','2015-06-08 12:59:00','2015 -06-08 14:58:00','2015-06-08 16:58:00','2015-06-08 18:59:00','2015-06-08 20:59:00', '2015-06-08 22:57:00','2015-06-09 00:59:00','2015-06-09 01:57:00','2015-06-09 03:22:00 ','2015-06-09 06:14:00','2015-06-09 08:59:00','2015-06-09 10:59:00','2015-06-09 12:59 :00','2015-06-09 14:59:00','2015-06-09 16:59:00','2015-06-09 18:59:00','2015-06-09 20 :59:00','2015-06-09 22:58:00','2015-06-10 00:57:00','2015-06-10 02:34:00','2015-06- 10 04:45:00','2015-06-10 06:24:00','2015-06-10 08:59:00','2015-06-10 10:59:00','2015- 06-10 12:59:00','2015-06-10 14:59:00','2015-06-10 16:59:00','2015-06-10 1 8:59:00','2015-06-10 20:58:00','2015-06-10 22:52:00','2015-06-11 00:59:00','2015-06 -11 02:59:00','2015-06-11 04:59:00','2015-06-11 06:59:00','2015-06-11 08:59:00','2015 -06-11 10:59:00','2015-06-11 12:59:00','2015-06-11 14:59:00','2015-06-11 16:58:00', '2015-06-11 18:58:00','2015-06-11 20:56:00','2015-06-11 21:49:00','2015-06-12 00:59:00 ','2015-06-12 02:59:00','2015-06-12 04:20:00','2015-06-12 08:55:00','2015-06-12 10:55 :00','2015-06-12 12:59:00','2015-06-12 14:59:00','2015-06-12 16:59:00','2015-06-12 18 :59:00','2015-06-12 20:55:00','2015-06-12 22:50:00','2015-06-13 00:16:00','2015-06- 13 12:59:00','2015-06-13 14:35:00','2015-06-13 16:56:00','2015-06-13 18:59:00','2015- 06-13 20:59:00','2015-06-13 22:44:00','2015-06-13 23:19:00','2015-06-14 08:53:00',' 2015-06-14 10:14:00','2015-06-14 12:59:00','2015-06-14 14:59:00','2015-06-14 16:56:00' , 2015-06-14 18:58:00','2015-06-14 20:57:00','2015-06-14 22:31:00','2015-06-14 23:59:00' )
count <-c(59,63,9,13,91,80,97,210,174,172,167,74,43,18,18,29,136,157,126,170,188,135,207,216,163,163,126,111,172,213,209,265,203,205,195,201,171,157,153,176,187,252,227,223,171,162,146,161,136,124,155,239,233,157,158,125,138,45,45,1,2,6,6,46,48 ('低','低','低','低','低','低'), , '低', '高', '正常', '正常', '正常的', '低', '低', '低', '低', '低', '低', '正常',低,‘正常’,‘正常’,‘低’,‘高’,‘高’,‘正常’,‘正常’,‘低’,‘低’,‘正常’,‘高’,‘高’ , '高', '高', '高', '正常的', '高', '正常', '正常', '正常', '正常', '正常的', '高', '高',高‘正常’,‘正常的’,‘低’,‘正常的’,‘低’,‘低’,‘正常的’,‘高’,高,‘正常’,‘正常的’,‘低’,‘低’,‘低’,‘低’,‘低’,‘低’,‘低’,‘低’,‘低’,‘低’ ,低,低,低,低,低,低,低,低,低,低等
DF = data.frame (时间,数量,水平)
DF $时间= as.POSIXct(DF $时间)

ggplot(DF,aes(x =时间,y = count,fill =等级), width = 0.9)+
geom_bar(stat =identity)+
scale_x_datetime(labels = date_format(%D),breaks = date_breaks(day))+
xlab myXlabel)+
ylab(myYlabel)+
ggtitle(myTitle)

解决方案

发现它!实际上,宽度是受支持的,尽管由于我绘制了X轴被格式化为POSIX日期的时间序列,因此刻度以秒为单位。因此,宽度= 0.9意味着垃圾箱宽度为0.9秒。因为我的箱子每个都是2小时,所以1的宽度实际上是7200.所以这里是代码的工作。

  ggplot (DF,aes(x = time,y = count,width = 6000,fill = level))+ 
geom_bar(stat =identity,position =identity,color =gray)+
scale_x_datetime(labels = date_format(%D),breaks = date_breaks(day))+
xlab(myXlabel)+
ylab(myYlabel)+
ggtitle(myTitle)

结果如下。在酒吧里有一些averlaps,我只需要将我的数据调整到下一个小时即可。


$ b


I would like to plot a time series using bar charts and have the Bin Width set to 0.9. I cannot seem to be able to do that however. I have searched around but could not find anything helpful so far. Is this a limitation if the stat="identity ?

Here is a sample data and graph. Cheers !

time <- c('2015-06-08 00:59:00','2015-06-08 02:48:00','2015-06-08 06:43:00','2015-06-08 08:59:00','2015-06-08 10:59:00','2015-06-08 12:59:00','2015-06-08 14:58:00','2015-06-08 16:58:00','2015-06-08 18:59:00','2015-06-08 20:59:00','2015-06-08 22:57:00','2015-06-09 00:59:00','2015-06-09 01:57:00','2015-06-09 03:22:00','2015-06-09 06:14:00','2015-06-09 08:59:00','2015-06-09 10:59:00','2015-06-09 12:59:00','2015-06-09 14:59:00','2015-06-09 16:59:00','2015-06-09 18:59:00','2015-06-09 20:59:00','2015-06-09 22:58:00','2015-06-10 00:57:00','2015-06-10 02:34:00','2015-06-10 04:45:00','2015-06-10 06:24:00','2015-06-10 08:59:00','2015-06-10 10:59:00','2015-06-10 12:59:00','2015-06-10 14:59:00','2015-06-10 16:59:00','2015-06-10 18:59:00','2015-06-10 20:58:00','2015-06-10 22:52:00','2015-06-11 00:59:00','2015-06-11 02:59:00','2015-06-11 04:59:00','2015-06-11 06:59:00','2015-06-11 08:59:00','2015-06-11 10:59:00','2015-06-11 12:59:00','2015-06-11 14:59:00','2015-06-11 16:58:00','2015-06-11 18:58:00','2015-06-11 20:56:00','2015-06-11 21:49:00','2015-06-12 00:59:00','2015-06-12 02:59:00','2015-06-12 04:20:00','2015-06-12 08:55:00','2015-06-12 10:55:00','2015-06-12 12:59:00','2015-06-12 14:59:00','2015-06-12 16:59:00','2015-06-12 18:59:00','2015-06-12 20:55:00','2015-06-12 22:50:00','2015-06-13 00:16:00','2015-06-13 12:59:00','2015-06-13 14:35:00','2015-06-13 16:56:00','2015-06-13 18:59:00','2015-06-13 20:59:00','2015-06-13 22:44:00','2015-06-13 23:19:00','2015-06-14 08:53:00','2015-06-14 10:14:00','2015-06-14 12:59:00','2015-06-14 14:59:00','2015-06-14 16:56:00','2015-06-14 18:58:00','2015-06-14 20:57:00','2015-06-14 22:31:00','2015-06-14 23:59:00')
count <- c(59,63,9,13,91,80,97,210,174,172,167,74,43,18,18,29,136,157,126,170,188,135,207,216,163,163,126,111,172,213,209,265,203,205,195,201,171,157,153,176,187,252,227,223,171,162,146,161,136,124,155,239,233,157,158,125,138,45,45,1,2,6,6,46,48,4,1,1,12,56,65,122,81,110,42)
level <- c('low','low','low','low','low','low','low','high','normal','normal','normal','low','low','low','low','low','low','normal','low','normal','normal','low','high','high','normal','normal','low','low','normal','high','high','high','high','high','normal','high','normal','normal','normal','normal','normal','high','high','high','normal','normal','low','normal','low','low','normal','high','high','normal','normal','low','low','low','low','low','low','low','low','low','low','low','low','low','low','low','low','low','low','low','low')
DF = data.frame(time, count, level) 
DF$time = as.POSIXct(DF$time)

ggplot(DF, aes(x=time, y=count, fill=level), width=0.9) + 
  geom_bar(stat="identity") + 
  scale_x_datetime(labels = date_format("%D"), breaks = date_breaks("day")) + 
  xlab("myXlabel") +
  ylab("myYlabel") +
  ggtitle("myTitle") 

解决方案

Found it ! Actually, the width is supported, though the scale is in seconds since I'm plotting a time series where the X axis is formatted as a POSIX date. Therefore, a width=0.9 means the bin width is 0.9 seconds. Since my bins are 2hrs eachs then a width of "1" is actually 7200. So here is the code that works.

ggplot(DF, aes(x=time, y=count, width=6000, fill=level)) + 
  geom_bar(stat="identity", position="identity", color="grey") + 
  scale_x_datetime(labels = date_format("%D"), breaks = date_breaks("day")) + 
  xlab("myXlabel") +
  ylab("myYlabel") +
  ggtitle("myTitle") 

Results as below. There are some averlaps in the bars, I just need to aligh my data, say to the next hour.

这篇关于如何使用geom_bar stat =“identity”设置Bin宽度在时间系列情节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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