如何在时间序列图中使用 geom_bar stat="identity" 设置 Bin 宽度?
How to set Bin Width With geom_bar stat="identity" in a time Series plot?
我想使用条形图绘制时间序列并将 Bin Width 设置为 0.9。但是我似乎无法做到这一点。我四处搜索但到目前为止找不到任何有用的东西。如果 stat="identity ?
,这是一个限制吗?
这是一个示例数据和图表。
干杯!
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")
如果您想要实现的是扩大图中的条形,ggplot 似乎不支持 geom_bar
。但是,使用 geom_rect
.
实现条形图非常简单
由于许多数据点似乎 spaced 大约相隔两个小时,我在这里假设你想要达到的 0.9 宽度是给定时间两侧的 0.9 小时(所以基本上是 smushing栏之间的大部分 space。
如果这是你想要的,下面的代码应该可以工作:
library(lubridate)
ggplot(DF, aes(xmin=time-minutes(54), xmax=time+minutes(54), ymin=0, ymax=count,
fill=level)) +
geom_rect(color="#666666")
找到了!实际上,宽度是受支持的,尽管比例以秒为单位,因为我正在绘制 X 轴格式为 POSIX 日期的时间序列。因此,width=0.9 表示 bin 宽度为 0.9 秒。因为我的垃圾箱每个都是 2 小时,所以宽度“1”实际上是 7200。所以这里是有效的代码。
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")
结果如下。酒吧里有一些平均,我只需要调整我的数据,说到下一个小时。
我也在努力研究 R。
我已经研究了一个解决方案并找到了一个解决方案,该解决方案还为我提供了指向问题的警告 - 重叠时间 x 间隔。错误在 width = 2000 处消失。通过补充 position = "dodge": "places overlapping objects directly aside one other" - https://r4ds.had.co.nz/data-visualisation.html - 你可以实现一个合理的情节。
# Original file
ggplot(DF, aes(x=time, y=count, fill=level, width=2000), position = "dodge") +
geom_bar(stat="identity") +
scale_x_datetime(labels = date_format("%D"), breaks = date_breaks("day")) +
xlab("myXlabel") +
ylab("myYlabel") +
ggtitle("myTitle")
以前的版本不太好这是另一个解决方案:
ggplot(DF, aes(x=time, y=count, colour = level)) +
geom_bar(stat="identity") +
scale_x_datetime(labels = date_format("%D"), breaks = date_breaks(width = "day")) +
xlab("myXlabel") +
ylab("myYlabel") +
ggtitle("myTitle")
colour=level 给出更宽的列
我想使用条形图绘制时间序列并将 Bin Width 设置为 0.9。但是我似乎无法做到这一点。我四处搜索但到目前为止找不到任何有用的东西。如果 stat="identity ?
,这是一个限制吗?这是一个示例数据和图表。 干杯!
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")
如果您想要实现的是扩大图中的条形,ggplot 似乎不支持 geom_bar
。但是,使用 geom_rect
.
由于许多数据点似乎 spaced 大约相隔两个小时,我在这里假设你想要达到的 0.9 宽度是给定时间两侧的 0.9 小时(所以基本上是 smushing栏之间的大部分 space。
如果这是你想要的,下面的代码应该可以工作:
library(lubridate)
ggplot(DF, aes(xmin=time-minutes(54), xmax=time+minutes(54), ymin=0, ymax=count,
fill=level)) +
geom_rect(color="#666666")
找到了!实际上,宽度是受支持的,尽管比例以秒为单位,因为我正在绘制 X 轴格式为 POSIX 日期的时间序列。因此,width=0.9 表示 bin 宽度为 0.9 秒。因为我的垃圾箱每个都是 2 小时,所以宽度“1”实际上是 7200。所以这里是有效的代码。
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")
结果如下。酒吧里有一些平均,我只需要调整我的数据,说到下一个小时。
我也在努力研究 R。 我已经研究了一个解决方案并找到了一个解决方案,该解决方案还为我提供了指向问题的警告 - 重叠时间 x 间隔。错误在 width = 2000 处消失。通过补充 position = "dodge": "places overlapping objects directly aside one other" - https://r4ds.had.co.nz/data-visualisation.html - 你可以实现一个合理的情节。
# Original file
ggplot(DF, aes(x=time, y=count, fill=level, width=2000), position = "dodge") +
geom_bar(stat="identity") +
scale_x_datetime(labels = date_format("%D"), breaks = date_breaks("day")) +
xlab("myXlabel") +
ylab("myYlabel") +
ggtitle("myTitle")
以前的版本不太好这是另一个解决方案:
ggplot(DF, aes(x=time, y=count, colour = level)) +
geom_bar(stat="identity") +
scale_x_datetime(labels = date_format("%D"), breaks = date_breaks(width = "day")) +
xlab("myXlabel") +
ylab("myYlabel") +
ggtitle("myTitle")
colour=level 给出更宽的列