我有四个数据集,我想在同一个图上绘制数据的直方图。我已将所有数据放入一个数据框中。我也可以在一个图上绘制直方图。但是,我无法绘制百分比而不是计数。当我使用下面的代码时,它将百分比绘制为所有计数的总和,但我希望百分比与每个数据集相关。这可能吗?
all <- rbind(data.frame(fill = "A", Events = A$Events),
data.frame(fill = "B", Events = B$Events),
data.frame(fill = "C", Events = C$Events),
data.frame(fill = "D", Events = D$Events)
ggplot(all,aes(x=Events, fill = fill)) +
geom_histogram(aes(y = ..count../sum(..count..)), position = 'dodge')
编辑
以下是一些示例数据:
fill Events
1 A 1
2 A 1
3 A 3
4 A 1
5 A 1
6 A 6
7 A 2
8 A 1
9 A 1
10 A 2
11 A 1
12 A 1
13 A 1
14 A 1
15 A 5
16 A 1
17 A 2
18 A 2
19 A 1
20 A 1
21 A 1
22 A 1
23 A 2
24 A 1
25 A 2
26 A 1
27 B 2
28 B 3
29 B 1
30 B 3
31 B 2
32 B 5
33 B 1
34 B 1
35 B 1
36 B 2
37 B 1
38 B 1
39 B 1
40 B 1
41 B 1
42 B 1
43 B 1
44 B 1
45 B 1
46 B 4
47 B 3
48 B 3
49 B 5
50 B 3
51 C 1
52 C 2
53 C 2
54 C 3
55 C 3
56 C 9
57 C 8
58 C 1
59 C 1
60 C 2
61 C 2
62 C 1
63 C 2
64 C 39
65 C 43
66 C 194
67 C 129
68 C 186
69 C 1
70 C 2
71 C 7
72 C 4
73 C 1
74 D 12
75 D 3
76 D 2
77 D 3
78 D 8
79 D 20
80 D 5
81 D 1
82 D 4
83 D 9
84 D 51
85 D 12
86 D 7
87 D 6
88 D 7
89 D 7
90 D 9
91 D 17
92 D 18
93 D 8
94 D 7
95 D 6
96 D 10
97 D 27
98 D 11
99 D 21
100 D 89
101 D 47
102 D 1