user3750840
user3750840

Reputation: 21

How to create circular time plots in R with 1 minute intervals

I am currently analysing data examining the behavioural response of BtRw to aerial survey based on time-lapse photography. Below is a short example of the data frames that I have imported into R.

Events are classified as the number of BtRw that were captured within a photo. I would really like to be able to create a circular time plot that represents the data at 1 minute intervals. I have tried alternate versions of the below code with limited success. The data I have was captured between 6:00am and 2:00 pm.

Any suggestions as to what I am doing wrong will be greatly appreciated.

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

Upvotes: 2

Views: 1543

Answers (1)

MrFlick
MrFlick

Reputation: 206421

So I don't think stat_histogram is a good choice because you have "collapsed" data here. So what I decided to do was convert the Time into a number (6*60 to 14*60) and then plot with geom_bar

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

And then plot that with

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

It still produces a warning (ie "position_stack requires non-overlapping x intervals") , but as far as I can tell that seems unavoidable. With in minute resolution, it's very hard to see individual bars in the plot already. Below is the plot created using the test data.

enter image description here

Upvotes: 1

Related Questions