2

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!

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Roben Seqer
  • 23
  • 1
  • 4
  • Just FYI, your `matrix` isn't a *matrix*--it's a data.frame. Matrices have to be all one data type (e.g., numeric) in R. – gung - Reinstate Monica Sep 06 '13 at 01:36
  • 1
    You're welcome, @Roben. If it helps you, I have a breakdown of that sort of thing here: [Learning R for someone used to MATLAB and confusion with R data types](http://stackoverflow.com/questions/15316580//15316581#15316581). – gung - Reinstate Monica Sep 06 '13 at 02:01
  • Have you tried to count your time in milliseconds and convert it to number, you can then used to count each column's size – Asayat Sep 06 '13 at 08:27
  • @Asayat I converted the time for hours but the value does not seem to match. In addition there are some roses that do not appear with the two colors - stacked rose -, only one. – Roben Seqer Sep 06 '13 at 08:34
  • How do you calculated in hours? if you just formatted to hours, it will show only %H number, so your first row will be 0. Probably that could be your case, if you have only 1 color in roses – Asayat Sep 06 '13 at 08:51

1 Answers1

2

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.

Community
  • 1
  • 1
Asayat
  • 645
  • 10
  • 23
  • Thanks, that's it! But the roses are overlapping each other, it is not possible to avoid overlapping in the graph? – Roben Seqer Sep 06 '13 at 10:55
  • my bad, change the width in geom_bar to smaller value, that should work – Asayat Sep 06 '13 at 11:02
  • I will accept the answer thanks! However the last rose (0/24) never appears, know what can be? – Roben Seqer Sep 06 '13 at 14:28
  • Add `+geom_text(aes(label=hour))` to see the labels for x axis, 24 is there. Again, it might be scaling issues, check the sum of total seconds spent for both occur values, time for 24th hour is 22835, while max is 134222. So rose (0/24) doesn't appear for the same reason why 4,5, and 16 doesn't. – Asayat Sep 06 '13 at 18:28