rggplot2rose-plot

Circular time plots in R with stacked rose


I have a data frame imported in excel with the following values:

> dt <- read.csv(file="teste1.csv",head=TRUE,sep=";")
> dt
   hour occur     time    tt
1     1   one 00:00:59    59
2     2   one 08:40:02 31202
3     3   one 07:09:59 25799
4     4   one 01:22:16  4936
5     5   one 01:30:28  5428
6     6   one 01:28:57  5337
7     7   one 19:05:34 68734
8     8   one 01:57:47  7067
9     9   one 00:13:17   797
10   10   one 12:14:48 44088
11   11   one 23:24:43 84283
12   12   one 13:23:14 48194
13   13   one 02:28:51  8931
14   14   one 14:21:24 51684
15   15   one 13:26:14 48374
16   16   one 00:27:24  1644
17   17   one 15:56:51 57411
18   18   one 11:07:50 40070
19   19   one 07:18:18 26298
20   20   one 07:33:13 27193
21   21   one 10:02:03 36123
22   22   one 11:30:32 41432
23   23   one 21:21:27 76887
24   24   one 00:49:18  2958
25    1   two 21:01:11 75671
26    2   two 11:00:40 39640
27    3   two 21:40:09 78009
28    4   two 01:05:37  3937
29    5   two 00:44:17  2657
30    6   two 12:43:21 45801
31    7   two 10:53:49 39229
32    8   two 08:29:09 30549
33    9   two 05:07:46 18466
34   10   two 17:32:37 63157
35   11   two 09:35:16 34516
36   12   two 03:04:19 11059
37   13   two 23:09:13 83353
38   14   two 01:15:49  4549
39   15   two 14:24:33 51873
40   16   two 01:12:53  4373
41   17   two 21:20:11 76811
42   18   two 02:25:21  8721
43   19   two 01:17:37  4657
44   20   two 15:07:50 54470
45   21   two 22:27:32 80852
46   22   two 01:41:07  6067
47   23   two 09:40:23 34823
48   24   two 05:31:17 19877

I want to create a circular time with stacked rose based on the data frame, ie, each stacked rose are grouped by column occur, and the size is defined by column time.

The column hour indicates the x position of each rose.

So I tried in this way but the result doesn't match with what I want:

ggplot(dt, aes(x = hour, fill = occur)) + geom_histogram(breaks = seq(0, 
    24), width = 2, colour = "grey") + coord_polar(start = 0) + theme_minimal() + 
    scale_fill_brewer() + scale_x_continuous("", limits = c(0, 24), breaks = seq(0, 24), labels = seq(0, 
        24))

What I'm doing wrong? I want something like this http://blog.odotech.com/Portals/57087/images/French%20landfill%20wind%20rose.png

I hope I've explained correctly. Thank you!


Solution

  • Not sure, but hope it helps:

    Convert your time value to numeric (I used chron package, but there are numerous other ways, so you don't have to call this library, but it's just to make it more straighforward):

    library(chron)
    x$tt<-hours(times(x$time))*3600+minutes(times(x$time))*60+seconds(times(x$time))
    

    And make a graph:

    p<-ggplot(x, aes(x = hour, y=tt,fill = occur)) + 
    geom_bar(breaks = seq(0,24), width = 2, colour="grey",stat = "identity") +
    theme_minimal() + 
    scale_fill_brewer()+coord_polar(start=0)+
    scale_x_continuous("", limits = c(0, 24), breaks = seq(0, 24), labels = seq(0,24))
    

    Is that ok? enter image description here

    Here some cases have only 1 colors, but it's due to the scaling issues, as some have time near 24 hours, while others are in seconds only. You can try separate graphs using facet (it's better to play with colors afterwards :))

    p+facet_grid(~occur)+ theme(axis.title.y = theme_blank(),
                           axis.text.y =  theme_blank()) 
    

    enter image description here

    The circular graph is good if you're comparing data by hours, but if you also want to compare differences in occur variable, think it's better to show in old fashion bar graphs.