简体   繁体   中英

bar plot with ggplot2: order the days-axis

I would like to get a multivariable bar graph using ggplot2 , where on the X-axis graph the sorted days of the week, on the Y-axis value , and fill the bars with the variable hour

> data
         day hour    value
1     sábado   22 3.795489
2     sábado   21 1.791759
3     sábado   20 3.072693
4     sábado   19 1.945910
5     sábado   18 3.332205
6     sábado   15 4.516339
7     sábado   13 4.219508
8     sábado   12 4.356709
9     sábado   11 2.980619
10    sábado    1 3.663562
11    sábado    0 3.650658
12   viernes   23 4.219508
13   viernes   22 3.113515
14   viernes   21 3.367296
15   viernes   18 3.218876
16   viernes   16 4.039536
17   viernes   14 3.277145
18    jueves   18 3.044522
19 miércoles   22 2.708050
20 miércoles   15 3.411148
21 miércoles   12 1.945910
22 miércoles   10 1.791759
23    martes   16 3.696351
24    martes   15 2.140066
25    martes    9 3.688879
26     lunes   18 1.791759
27     lunes   16 3.650658
28     lunes   13 2.442347
29     lunes   10 3.589059
30   domingo   10 4.127134
31   domingo    0 4.872905
32    sábado   23 3.751854
33   viernes   20 2.302585
34   viernes    9 3.569533
35    jueves   12 2.197225
36 miércoles   18 3.332205
37    martes   14 3.295837
38    martes   13 3.465736
39    martes   10 2.424803
40     lunes   21 4.200205
41     lunes   15 2.564949
42     lunes   12 4.248495
43   domingo   21 4.494239
44   domingo   20 3.822098
45   domingo   12 4.806477
46   viernes   15 5.181784
47   viernes   13 5.308268
48   viernes   12 3.178054
49   viernes   11 5.173321
50   viernes   10 4.410371
51    jueves   23 2.890372
52    jueves   22 2.251292
53    jueves   20 2.917771
54    jueves    9 4.174387
55 miércoles    9 3.663562
56    martes   21 1.945910
57 miércoles   21 2.079442
58 miércoles   13 3.850148
59     lunes   17 4.629863
60     lunes    9 4.532599
61   domingo   19 3.496508
62   domingo   13 3.688879

I run this R code, but I want to order the days, and fill bars with hours (in function of decreasing values for variable value, for example, on the TOP must appears the hour which has longer value ).

ggplot(arrange(data,day),aes(x = day, y = value))  +
  geom_bar(stat="identity",size=1.5,aes(fill=hour)) 

在此处输入图片说明

You can order the hour in desc

ggplot(arrange(data,day),aes(x = day, y = value))  +
+     geom_bar(stat="identity",size=1.5,aes(fill=order(-hour))) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM