如何在R中创建循环时间图,间隔为1分钟

时间:2014-06-18 04:06:44

标签: r ggplot2 rose-diagram

我目前正在分析基于延时摄影检查BtRw对航测的行为反应的数据。下面是我导入R的数据框的简短示例。

事件被分类为照片中捕获的BtRw的数量。我真的希望能够创建一个循环时间图,以1分钟为间隔表示数据。我尝试过以下代码的替代版本,但成效有限。我的数据是在上午6:00到下午2:00之间捕获的。

对于我所做错的任何建议都将不胜感激。

ggplot(eventdata, aes(x=eventhour, fill = Events)) + 
geom_histogram(breaks = seq(6, 14), width = 2, colour = "grey") + 
coord_polar(start = 6) + theme_minimal() + scale_fill_brewer() + ylab ("Count") +
ggtitle("Events by Time of Day") + 
scale_x_continuous("", limits = c(6,14), breaks = seq(6, 14), labels = seq(6, 14))

        Time       Events 

131     8:09:00      2
 132     8:10:00      2
 133     8:11:00      2
 134     8:12:00      4
 135     8:13:00      4
 136     8:14:00      5
 137     8:15:00      5
 138     8:16:00      5
 139     8:17:00      5
 140     8:18:00      5
 141     8:19:00      5
 142     8:20:00      6
 143     8:21:00      6
 144     8:22:00      6
 145     8:23:00      5
 146     8:24:00      5
 147     8:25:00      5
 148     8:26:00      6
 149     8:27:00      6
 150     8:28:00      5
 151     8:29:00      5
 152     8:30:00      5
 153     8:31:00      5
 154     8:32:00      5
 155     8:33:00      5
 156     8:34:00      6
 157     8:35:00      6
 158     8:36:00      6
 159     8:37:00      6
 160     8:38:00      5
 161     8:39:00      6
 162     8:40:00      6
 163     8:41:00      6
 164     8:42:00      6
 165     8:43:00      6
 166     8:44:00      5
 167     8:45:00      4
 168     8:46:00      4
 169     8:47:00      4
 170     8:48:00      4
 171     8:49:00      4
 172     8:50:00      4
 173     8:51:00      4
 174     8:52:00      4
 175     8:53:00      4
 176     8:54:00      4
 177     8:55:00      4
 178     8:56:00      4
 179     8:57:00      4
 180     8:58:00      4
 181     8:59:00      4
 182     9:00:00      4
 183     9:01:00      4
 184     9:02:00      4
 185     9:03:00      4
 186     9:04:00      4
 187     9:05:00      4
 188     9:06:00      4
 189     9:07:00      4
 190     9:08:00      3
 191     9:09:00      3
 192     9:10:00      3
 193     9:11:00      3

1 个答案:

答案 0 :(得分:1)

所以我不认为stat_histogram是一个不错的选择,因为你已经崩溃了#34;数据在这里。所以我决定将时间转换为数字(6 * 60到14 * 60),然后使用geom_bar进行绘图

eventdata$minute <- sapply(strsplit(as.character(eventdata$Time), ":", fixed=T), 
    function(x) {as.numeric(x[1])*60+as.numeric(x[2])})

然后用

绘制
ggplot(eventdata, aes(x=minute, y=Events, fill=as.factor(Events))) + 
    geom_bar(stat="identity", width=2) + 
    coord_polar(start = 6*60) + theme_minimal() + scale_color_brewer() + 
    ggtitle("Events by Time of Day") + ylab ("Count") +
    scale_x_continuous("", limits = c(6,14)*60, breaks = seq(6, 13)*60, 
        labels = paste0(seq(6, 13),":00"))

它仍然会产生一个警告(即&#34; position_stack需要不重叠的x区间&#34;),但据我所知,这似乎是不可避免的。在分辨率很高的情况下,很难看到情节中的单个条形图。下面是使用测试数据创建的图。

enter image description here