R Plotly - 堆积条形图无法正确分组数据

R Plotly - Stacked Bar Chart failing to group data properly

我确定这是一个相对简单的问题,但我发现它很乏味。

我正在尝试在 Plotly 中绘制一个交互式堆积条形图,其中日期横跨 x 轴,床位数在 Y 轴上,按评级分组。下面我包括了 str()、head()、我的代码和现有的输出。

不胜感激。

我觉得最好的方法是以某种方式按日期汇总数据,以及特定评级的床位总和 - 但我不确定我将如何处理这个...

str()

'data.frame':   3492 obs. of  4 variables:
 $ Date    : Date, format: "2016-05-01" ...
 $ Rating  : Factor w/ 5 levels "Good","Inadequate",..: 1 1 1 1 4 1 1 1 1 4 ...
 $ Beds    : num  4 50 0 41 7 0 0 6 4 26 ...
 $ Location: chr  "73ecf8c8" "8b42c57b" "ba87a0b9" "56c5af23" ...

head()

              Date               Rating Beds Location
845296 2016-05-01                 Good    4 73ecf8c8
531140 2017-07-01                 Good   50 8b42c57b
258467 2016-12-01                 Good    0 ba87a0b9
717015 2017-03-01                 Good   41 56c5af23
26558  2016-08-01 Requires improvement    7 2a3b1665
32035  2017-11-01                 Good    0 19a428f8
537744 2017-12-01                 Good    0 f238d5b0
132722 2016-05-01                 Good    6 281b0906
141156 2017-04-01                 Good    4 b9a84e23
448129 2017-02-01 Requires improvement   26 b17b1679
215566 2017-01-01                 Good   39 3deb6a53
93926  2017-04-01                 Good   42 ea25b9ac
50825  2016-10-01 Requires improvement   38 a123e5e2
350187 2017-09-01                 Good    0 20a5b3ff
228490 2015-12-01                 Good    5 26bbe2d3
619449 2017-09-01 Requires improvement   64 4f1850cb
277463 2015-11-01 Requires improvement   60 da021815
747589 2018-07-01                 Good    0 b680e86a
572946 2018-07-01                 Good   36 8f768366
130828 2016-08-01                 Good   14 26140cb7
176208 2017-01-01                 Good    8 90ea5a9b
131422 2015-09-01                 Good   34 3f710dda
314684 2017-07-01 Requires improvement    0 542ecec0
27104  2017-10-01 Requires improvement   14 a0a39a69
385850 2018-02-01           Inadequate   12 2c2157a3
131793 2017-06-01 Requires improvement   40 c5279a90
476514 2018-03-01                 Good   61 1ede253c
234275 2017-12-01                 Good    3 5b843535
814308 2016-08-01                 Good    0 8e8ae05c
681168 2018-01-01                 Good   19 c7503f95
228857 2016-03-01                 Good    4 b79ec2e1
5076   2016-12-01                 Good    9 c4571226
516624 2018-03-01                 Good    4 3c4a60e8
328782 2016-11-01                 Good    8 509886d8
454544 2017-10-01                 Good    8  551e223
273560 2016-06-01 Requires improvement   57 fa936153
734068 2017-12-01                 Good    0 1be00d0a
316369 2016-01-01           Inadequate   27 6a70309d
818050 2016-12-01                 Good    0 feaefffd
99811  2017-03-01                 Good    7 2832404f
376905 2016-10-01 Requires improvement    0 45494c27
18056  2018-02-01 Requires improvement   15 92d0f90e
183216 2017-04-01                 Good   20 52061c2b
139057 2016-09-01 Requires improvement   42 38d2867b
349667 2016-09-01                 Good   94 ae061300
538397 2018-04-01                 Good   19 fe926d3c
70481  2015-11-01 Requires improvement   14  d889131
749222 2017-11-01                 Good    0 990f6742
64034  2018-01-01                 Good    8 c79a60e3
154178 2018-02-01                 Good   66 74192594
85413  2017-01-01                 Good   33 61bd8955
286676 2018-01-01 Requires improvement   29 289ee997
838167 2018-07-01                 Good    0 3e3be7b6
594222 2017-05-01                 Good   45 73149b93
341018 2017-06-01                 Good   80 9c79bed4
87187  2016-11-01                 Good    7 6511d087
57191  2016-10-01 Requires improvement    0 489cccef
572701 2017-05-01                 Good    0 9e5b7624
433898 2017-02-01 Requires improvement   36 9fb506e9
147450 2016-09-01                 Good   51 f755015c
147465 2017-12-01                 Good   51 f755015c
768482 2018-04-01                 Good   35 2ec82812
164492 2016-11-01                 Good   29 37b6722e
317253 2018-05-01                 Good   51 330236c2
106128 2015-09-01                 Good   14 73fc9b5d
719658 2018-03-01                 Good   44 d01cd06a
22099  2016-01-01                 Good   25 1278109f
25584  2018-03-01                 Good   16 f7949387
128071 2016-01-01                 Good   61 6b5c264d
114202 2017-07-01                 Good    0 acc76673
212877 2017-10-01                 Good    0 8435c62b
838998 2017-03-01 Requires improvement   21 d107525f
534825 2016-08-01                 Good   34 f33ed6dd
810734 2017-11-01           Inadequate    0 cef20cb9
93621  2017-05-01                 Good   23 76cc5458
855084 2016-01-01                 Good    0 343f87e1
290641 2017-02-01                 Good   76 7c96b559
422625 2016-02-01 Requires improvement   44 38d19478
698519 2018-05-01                 Good    0  7e37c12
451978 2018-01-01                 Good   28 5c85d81e
277454 2018-01-01                 Good   30 6c01aba2
327926 2017-06-01                 Good    8 4b3ef025
405035 2018-06-01                 Good   51 5fb1a56f
458644 2017-11-01                 Good   13 8fea9b31
101605 2016-06-01                 Good    0 4849d79b
852084 2016-10-01                 Good    6 3158ee3e
795288 2016-01-01                 Good    0 ae9f7110
218899 2017-02-01                 Good   14 2f77717a
352636 2017-11-01          Outstanding    4 3b8be9df
519895 2018-04-01 Requires improvement   33 1cda38ff
434329 2015-12-01 Requires improvement   62 669494f8
517045 2018-04-01                 Good    0 3f317755
293071 2017-01-01                 Good   16 e2283b4d
109754 2017-12-01                 Good   40 6a4e3d80
833849 2018-05-01                 Good    0 24c1fd1f
544942 2017-12-01                 Good   40 6e9808b5
42046  2016-08-01 Requires improvement    3 b9c2bf48
264610 2016-05-01                 Good   58 e3f2f21b
360231 2018-04-01                 Good   26 8e4b7c51
405704 2016-11-01                 Good   19 3224233f
729533 2016-01-01                 Good   40 3f816ee2
587697 2017-01-01 Requires improvement    0 8a107710
802252 2018-04-01                 Good    0 1be00d0a
722684 2017-04-01                 Good    0 c29c49d0
506127 2017-02-01 Requires improvement    0 a4584633
224827 2017-01-01                 Good    0  fbf59fb
532047 2017-06-01                 Good    0 ccb9197a
30224  2016-04-01                 Good    7 57ddb719
313612 2017-03-01                 Good   54 fd3b1c23
437224 2017-08-01                 Good   10 1cae80aa
39140  2018-03-01                 Good    9 50828278
673780 2017-04-01                 Good    0 4e09a7e3
713658 2016-07-01                 Good    5 4cf5b0c8
356121 2018-05-01                 Good   42  ca78522
266006 2016-07-01                 Good    0 8a123db2
137548 2017-05-01                 Good    4 ee22dee9
302152 2016-08-01 Requires improvement  120 be5c07bc
107929 2016-01-01 Requires improvement   40 ad634df3
129558 2016-12-01                 Good    6 7d76fe74
220591 2018-03-01                 Good    5 31c4f6f5
410000 2016-09-01                 Good    0 8536f1ae
512051 2017-04-01                 Good    0  e2dbda9
669160 2017-03-01                 Good    0 5d0dd1c9
502046 2017-11-01           Inadequate   16 c44f2103
778049 2016-12-01                 Good    9 d9df8a9b
327324 2017-06-01                 Good    6 82925f7a
10338  2017-01-01                 Good   23 9367642e
380084 2016-07-01                 Good    0 26dba9aa
177099 2017-10-01                 Good   29 55c5b0c4
451721 2017-03-01                 Good   10 adabb9bd
148916 2016-12-01 Requires improvement   33 a4b1aed0
331453 2017-03-01                 Good   31 c8c635e4
215781 2015-11-01                 Good   54 fc3ec716
703935 2016-05-01 Requires improvement   60 e864967b
422946 2016-08-01 Requires improvement   20 57686493
689546 2018-06-01 Requires improvement    0 d187188c
81389  2017-08-01                 Good   15 c6876e99
217176 2017-12-01                 Good   54 dbc379c1
861416 2015-12-01 Requires improvement    3 3e421008
182407 2016-12-01                 Good   11 db975de3
830441 2016-04-01                 Good   60 8c435ffd
395178 2017-12-01                 Good   46 ca8ec055
279329 2016-10-01                 Good    0 41fde67d
286571 2017-05-01                 Good    0 814050b3
519878 2016-11-01 Requires improvement   33 1cda38ff
356862 2018-07-01                 Good    0 23df4b6a
349400 2017-10-01                 Good   25 345b540d
424046 2017-04-01 Requires improvement   22 ac9c36ec
703641 2018-06-01                 Good   11 b9d33a8c
158566 2018-02-01                 Good    0 ece2a325
805260 2018-02-01                 Good   50  ec13225
266666 2015-10-01 Requires improvement   24 3b276650
489618 2018-02-01                 Good   15 86171b0c
253591 2018-03-01                 Good   49 1971d20a
158697 2017-04-01 Requires improvement   23 3b0964d5
702350 2017-07-01                 Good   33 6660201e
221603 2016-09-01                 Good    5 d0ee1cb0
629620 2018-03-01                 Good    0 ad593a5e
364422 2017-08-01                 Good    5 10d07106
209910 2017-10-01                 Good   45 becda3b9
805251 2017-05-01 Requires improvement   50  ec13225
48490  2017-07-01 Requires improvement  129 b961e4b9
653676 2017-11-01                 Good    0  b009efa
104525 2016-11-01                 Good    4 af2447cf
136762 2017-08-01                 Good   22 3ab84c39
301929 2017-11-01                 Good    6 6a9754db
254622 2017-01-01                 Good    5 62d29e47
171199 2017-01-01                 Good    9 d6b557e5
308962 2017-07-01          Outstanding    0 a1c3486d
679497 2018-03-01 Requires improvement   19 f0b69678
583701 2018-01-01                 Good    2 7bed9aee
818816 2015-09-01 Requires improvement   80 ee56de08
470911 2016-06-01                 Good   31 df3e2263
324680 2016-01-01                 Good    7 6f14dc6e
286625 2016-09-01                 Good    9 82987732
851305 2017-11-01 Requires improvement    0  e644f19
431351 2017-09-01                 Good   75 cab33e6c
377702 2017-10-01                 Good   10 cb16e04f
659315 2017-12-01                 Good   46 cd3ed3dd
635446 2017-11-01                 Good    3 9d2079f3
723729 2018-07-01                 Good   10 e844216b
568093 2017-09-01                 Good   28 64585bf9
43025  2016-02-01 Requires improvement    0 bd8776e3
304749 2017-04-01                 Good   17 88bd81a5
104726 2018-01-01                 Good   32 4b3eb01e
102932 2016-06-01 Requires improvement   52  8854302
320747 2016-09-01 Requires improvement    6 ea96ec94
211455 2017-11-01                 Good    7 960ff19f
365985 2018-07-01                 Good    6 2d61675f
112236 2017-02-01                 Good    8 18917c87
321448 2016-04-01                 Good    8 a2020b57
252145 2017-04-01                 Good    4 856d0879
592746 2018-03-01                 Good    0  b4fcf4c
92992  2016-05-01                 Good   15 c7652688
242329 2016-07-01 Requires improvement   34 99205575
482507 2016-01-01 Requires improvement   59 1bf96a6e
438733 2016-09-01 Requires improvement    0 9bd0afa6
849800 2016-02-01                 Good    0 d03c7ca7
665019 2018-01-01 Requires improvement    0 1e370102
55080  2016-07-01                 Good    6 b7a5aa66
326518 2018-07-01                 Good    3 a59654e7
744501 2017-08-01                 Good    5 4f23064f
810313 2017-09-01                 Good   17 ce5f8661
859849 2016-03-01           Inadequate    0 7e2226fd
552668 2017-12-01                 Good   21 94b3b830
657502 2017-03-01                 Good   19 bc403e1b
837974 2016-10-01                 Good   18 70db26b0
552564 2018-01-01                 Good    0 df5a1352
422645 2017-10-01                 Good   44 38d19478
408661 2016-12-01                 Good   26  36a954d
234622 2018-04-01 Requires improvement    0  768a842
118553 2018-02-01                 Good   37 3c7b7179
68081  2017-09-01                 Good   35 744d32bd
164336 2018-06-01                 Good   29 c1fcb144
85725  2018-04-01 Requires improvement   24 341c2894
771416 2018-03-01 Requires improvement    0 f4b7202b
811557 2018-03-01                 Good   50 2c99c463
356332 2016-02-01 Requires improvement   24 893bd8f1
271763 2016-07-01                 Good   55 b4973db4
844597 2018-03-01                 Good   14 dd7afdc4
108342 2017-12-01                 Good    0 3e7910a8
397322 2017-04-01                 Good    6 2eea4528
279122 2018-06-01                 Good   90 64256055
326607 2017-03-01                 Good    3 f95fba06
12693  2017-09-01                 Good    0 2c1ab4c8
835690 2016-07-01 Requires improvement   46 912e4df4
500979 2016-09-01                 Good    6  d001d2f
228199 2016-06-01                 Good   16 a63ca739
317800 2015-12-01 Requires improvement   50 3dbd50ea
406448 2016-01-01           Inadequate    9 a877167e
383427 2015-10-01                 Good   86 ad4d7ba2
336054 2017-04-01 Requires improvement   80 e47a766e
746245 2015-10-01                 Good   52 b71907f1
552129 2016-04-01                 Good    0 9f418780
525654 2016-04-01                 Good    0 8369a68f
132864 2016-07-01                 Good   15 70340e4b
67588  2016-12-01 Requires improvement    5 d8d8e1f9
608797 2015-10-01                 Good    4 5ab14628
475296 2016-08-01 Requires improvement   35 1f637bf2
163400 2015-11-01 Requires improvement   35 5d6f7386
550891 2018-06-01                 Good   30 4c6f5407
270228 2018-04-01                 Good   16 21df63bf
60885  2017-10-01                 Good   40 6c7ab064
685038 2017-06-01                 Good   30 686c921f
466925 2017-04-01                 Good    1 8b4e8494
835711 2018-04-01           Inadequate   46 912e4df4
523228 2017-02-01                 Good    0 fac83fc1
309203 2017-11-01                 Good   69 79864c4f
290655 2018-04-01 Requires improvement   76 7c96b559
847687 2018-05-01                 Good    0 9cd483a1

现有代码

plot_ly(test2, 
        x = ~Date, y = ~Beds, color = ~Rating, marker = col, type = "bar"
        )%>%
   layout(barmode = "stack")

问题图片

尝试使用 dplyr::count 来执行此操作(与 plyr::count function 略有不同)。下面是使用 ggplotly 函数的示例。

这个函数基本上使用 group_bysum()n() 和权重 (wt) 汇总数据,然后 ungroup() 都在一个命令中。 由于每个日期已经有多个床位计数(在评级内),因此计数需要 "weighted" 乘以 Beds

test2 <- test2 %>% 
  mutate(Date = lubridate::ymd(Date),
         Rating = factor(Rating))
test2 %>% 
  dplyr::count(Date, wt=Beds, Rating) %>% 
  ggplot(aes(Date, n, fill = Rating)) +
  geom_bar(stat = "identity") 
ggplotly()