簡體   English   中英

ggplot2圖,用於評估相交點處第三個變量的百分比和均值

[英]ggplot2 plot that evaluates the percentage and mean of a third variable at intersecting points

我有一組患者列表,兩個類別變量,一個結果和一個住院時間的數據集。 該數據的示例如下:

ID,A,B,Outcome,LOS
1,5,4,Died in Hospital,20.9
2,4,4,Transferred to other acute hospital,2096.47
3,5,4,Transferred to other acute hospital,1258.25
4,2,4,Transferred to other acute hospital,337.37
5,5,4,Died in Hospital,8.13
6,5,4,Died in Hospital,17.53
7,5,4,Transferred to other acute hospital,904.02
8,4,3,Died in Hospital,1425.9
9,5,4,Died in Hospital,21.77
10,2,4,Transferred to other acute hospital,1017.3
11,5,4,Transferred to other acute hospital,1009.08
12,1,3,Discharged Home,573.25
13,5,4,Died in Hospital,170.97
14,5,4,Died in Hospital,85
15,5,4,Discharged Home,476.65
16,5,4,Died in Hospital,42.48
17,5,4,Transferred to other acute hospital,1512.35
18,1,4,Transferred to other acute hospital,777.77
19,2,4,Died in Hospital,1104.75
20,3,4,Transferred to other acute hospital,699.08
21,4,4,Died in Hospital,1859.93
22,5,4,Discharged Home,781.73
23,5,4,Discharged Home,477.42
24,4,4,Transferred to other acute hospital,1424.47
25,5,4,Transferred to other acute hospital,1284.98
26,5,4,Transferred to rehab facility,1783.27
27,2,4,Transferred to other acute hospital,886.82
28,2,4,Died in Hospital,577.85
29,5,4,Discharged Home,549.98
30,4,4,Transferred to other acute hospital,2271.1
31,5,4,Died in Hospital,169.82
32,5,4,Transferred to other acute hospital,736.82
33,5,4,Died in Hospital,548.7
34,5,4,Transferred to other acute hospital,481.67
35,4,4,Transferred to other acute hospital,887.18
36,3,4,Discharged Home,674.63
37,4,4,Transferred to other acute hospital,1600
38,5,4,Died in Hospital,50.1
39,2,4,Died in Hospital,169.85
40,5,4,Transferred to other acute hospital,1595.85
41,5,4,Died in Hospital,8.15
42,2,4,Discharged Home,371.37
43,5,4,Died in Hospital,26.13
44,5,4,Died in Hospital,196.42
45,5,4,Died in Hospital,117.38
46,5,4,Died in Hospital,263.78
47,5,4,Discharged Home,613.22
48,2,4,Discharged Home,255.85
49,5,4,Died in Hospital,37.05
50,3,4,Discharged Home,2052.55
51,5,4,Died in Hospital,51.95
52,4,4,Transferred to other acute hospital,714.12
53,3,4,Transferred to other acute hospital,687.05
54,1,3,Transferred to other acute hospital,1411.37
55,4,4,Discharged Home,445.12
56,2,4,Discharged Home,233.08
57,5,4,Transferred to other ICU,137.07
58,2,4,Discharged Home,346.73
59,5,4,Died in Hospital,96.88
60,4,4,Discharged Home,511.43
61,5,4,Transferred to other acute hospital,1067.02
62,5,4,Discharged Home,810.68
63,1,4,Discharged Home,385.98
64,3,4,Died in Hospital,457.73
65,2,3,Discharged Home,684.37
66,4,4,Discharged Home,475.68
67,3,4,Discharged Home,2693.92
68,3,4,Discharged Home,333.58
69,1,4,Discharged Home,249.32
70,2,4,Discharged Home,282.3
71,1,3,Discharged Home,573.22
72,1,2,Discharged Home,441.98
73,1,3,Discharged Home,396.48
74,1,3,Discharged Home,886.4
75,2,4,Discharged Home,651
76,3,4,Transferred to other acute hospital,967.57
77,4,3,Discharged Home,557.23
78,1,2,Transferred to other acute hospital,473.37
79,2,4,Discharged Home,399.85
80,2,4,Discharged Home,495.7
81,1,4,Transferred to other acute hospital,779.83
82,2,4,Discharged Home,301.42
83,3,4,Died in Hospital,562.37
84,3,4,Discharged Home,2589.27
85,3,4,Transferred to other acute hospital,700
86,5,4,Transferred to other acute hospital,763.57
87,3,4,Discharged Home,584.85
88,2,4,Discharged Home,705.72
89,1,3,Discharged Home,581.08
90,2,4,Transferred to other acute hospital,565.3
91,2,2,Discharged Home,306.25
92,2,2,Discharged Home,468.7
93,3,4,Discharged Home,344.8
94,4,4,Discharged Home,319.28
95,2,3,Discharged Home,427.6
96,2,1,Discharged Home,244.42
97,4,3,Discharged Home,405.22
98,3,3,Died in Hospital,398.7
99,1,4,Transferred to other acute hospital,607.2
100,2,4,Discharged Home,470.98
101,2,2,Discharged Home,325.22
102,1,3,Discharged Home,448.72
103,4,4,Transferred to other acute hospital,499.55
104,2,2,Transferred to other acute hospital,473.7
105,2,4,Died in Hospital,908.2
106,1,3,Discharged Home,372.77
107,2,3,Discharged Home,360.63
108,1,3,Discharged Home,303.63
109,1,4,Transferred to other acute hospital,2797.98
110,2,4,Discharged Home,588.75
111,4,4,Transferred to other acute hospital,1078.32
112,3,3,Transferred to other acute hospital,350.55
113,5,4,Died in Hospital,106.83
114,1,4,Transferred to other acute hospital,851.58
115,1,2,Discharged Home,342.47
116,3,3,Discharged Home,517.67
117,2,4,Discharged Home,2117.82
118,3,4,Discharged Home,610.68
119,2,3,Discharged Home,561.47
120,2,3,Discharged Home,359.87
121,1,2,Discharged Home,473.95
122,5,4,Discharged Home,832.12
123,1,4,Discharged Home,396.97
124,1,4,Transferred to rehab facility,859.65
125,1,2,Transferred to other acute hospital,585.97
126,4,4,Discharged Home,494.37
127,3,3,Died in Hospital,157.87
128,3,4,Discharged Home,358.18
129,2,4,Transferred to other acute hospital,1001.02
130,1,1,Discharged Home,334.82
131,2,2,Discharged Home,269.98
132,2,2,Discharged Home,326.5
133,1,4,Transferred to other acute hospital,496
134,1,4,Transferred to other acute hospital,362.35
135,1,2,Discharged Home,226.82
136,1,2,Transferred to other acute hospital,517.1
137,2,3,Discharged Home,330.87
138,1,3,Discharged Home,308.82
139,1,1,Discharged Home,428.63
140,1,3,Discharged Home,239.25
141,3,4,Discharged Home,389.92
142,1,1,Discharged Home,187.32
143,1,3,Transferred to other acute hospital,167.02
144,1,2,Discharged Home,300.4
145,1,1,Discharged Home,155.57
146,1,2,Discharged Home,218.13
147,2,3,Discharged Home,446.57
148,1,2,Discharged Home,132.55
149,2,2,Discharged Home,258.37
150,2,3,Discharged Home,298.18
151,1,1,Discharged Home,176.38
152,1,4,Discharged Home,723.37
153,1,1,Discharged Home,542.2
154,1,1,Discharged Home,329.43
155,2,3,Died in Hospital,120.18
156,1,4,Transferred to other acute hospital,207.9
157,2,2,Discharged Home,246.75
158,1,3,Discharged Home,308.87
159,2,2,Discharged Home,122.4
160,1,4,Transferred to other acute hospital,1440.27
161,1,2,Discharged Home,332.28
162,1,2,Discharged Home,224.18
163,1,3,Discharged Home,256.65
164,3,4,Discharged Home,939.55
165,2,2,Discharged Home,809.87
166,1,3,Discharged Home,335.62
167,1,3,Discharged Home,285.93
168,1,4,Discharged Home,9319.17
169,2,3,Discharged Home,328
170,1,3,Discharged Home,235.78
171,1,2,Discharged Home,215.55
172,1,4,Discharged Home,421.65
173,1,2,Discharged Home,258.5
174,1,3,Discharged Home,347.48
175,1,3,Discharged Home,327.73
176,1,4,Discharged Home,265.37
177,1,2,Discharged Home,107.37
178,2,3,Discharged Home,236.17
179,1,2,Discharged Home,180
180,1,2,Discharged Home,84.3
181,1,2,Discharged Home,133.87
182,1,3,Discharged Home,311.03
183,1,2,Discharged Home,305
184,1,3,Discharged Home,400.3
185,1,3,Discharged Home,324
186,1,1,Transferred to other acute hospital,427.12
187,1,2,Discharged Home,284.62
188,1,4,Died in Hospital,235.73
189,1,3,Discharged Home,173.55
190,2,3,Discharged Home,339.6
191,1,2,Discharged Home,448.65
192,1,3,Transferred to rehab facility,252.8
193,1,3,Discharged Home,374.4
194,1,3,Discharged Home,180.52
195,1,4,Discharged Home,403.33
196,2,2,Transferred to other acute hospital,56.97
197,1,2,Discharged Home,378.07
198,3,1,Discharged Home,350.07
199,1,1,Discharged Home,293.07
200,1,2,Discharged Home,67.67
201,1,3,Discharged Home,81.6
202,2,3,Discharged Home,436.35
203,1,2,Discharged Home,276.27
204,3,4,Discharged Home,658.08
205,1,2,Transferred to other acute hospital,266.2
206,1,1,Discharged Home,280.97
207,1,4,Discharged Home,324.07
208,2,3,Discharged Home,293.85
209,1,2,Discharged Home,425.32
210,1,3,Discharged Home,185.77
211,1,4,Transferred to other acute hospital,87.75
212,1,3,Discharged Home,170.28
213,1,4,Discharged Home,352.6
214,2,3,Transferred to other ICU,8.25
215,2,4,Discharged Home,382.15
216,1,4,Discharged Home,429.13
217,2,2,Discharged Home,375.03
218,2,4,Discharged Home,1157.8
219,1,2,Discharged Home,410.93
220,1,1,Discharged Home,343.03
221,1,2,Discharged Home,324.55
222,1,1,Discharged Home,120.55

我試圖繪制一個以A和B為軸的圖,在A和B的每個組合處顯示死於該組合的百分比。 在另一個繪圖中,我試圖制作一個以A和B為軸的繪圖,在A和B的每個組合處均顯示該組合的平均停留時間。

例如,在B = 4 A = 2時,總共有24位患者,其中4位死亡。 然后,我希望此時顯示4/24 = 16.7%。

我已經嘗試使用stat_sum(過去在類似情況下非常有用,但是在這里我沒有發現它有用。

我嘗試過的代碼是:

dx$died <- ifelse(dx$Outcome=="Died in Hospital", 1, 0)
bubbleDeath <- ggplot(dx, aes(x=B, y=A)) +
stat_sum(aes(group=1, weight=died, label = paste(round(..prop.. * 100, 2), "%", sep = "")), geom = "text", show_guide = FALSE)
print(bubbleDeath)

這沒有用,因為它將它作為一個整體而不是單個點來評估。 我玩過ddply,但是一無所獲!

提前非常感謝您。

這概括了解決方案,並且還使用了熱圖,而不僅僅是純文本:

library(dplyr)
library(scales)
library(ggplot2)
library(RColorBrewer)

dat_sum <- dat %>% 
  mutate(A=factor(A), 
         B=factor(B)) %>%
  group_by(B, A, Outcome) %>%
  summarise(outcome_total=n()) %>%
  mutate(freq=outcome_total/sum(outcome_total)) %>%
  merge(expand(., B, A, Outcome), all.y=TRUE) %>%
  mutate(outcome_total=ifelse(is.na(outcome_total), 0, outcome_total),
         freq=ifelse(is.na(freq), 0, freq),
         fill_col=cut(freq, 
                      breaks=seq(0,1,.1),
                      include.lowest=TRUE))

dat_sum %>%
  ggplot(aes(x=B, y=A)) +  
  geom_tile(aes(fill=fill_col), color="#7f7f7f") +
  geom_text(aes(label=percent(freq), color=fill_col)) +
  scale_fill_manual(values=c("white", brewer.pal(n=9, name="BuGn"))) +
  scale_color_manual(values=c(rep("black", 6), rep("white", 3))) +
  facet_wrap(~Outcome, ncol=3) +
  theme_bw() +
  theme(legend.position="none") +
  theme(panel.grid=element_blank()) +
  theme(strip.background=element_blank())

在此處輸入圖片說明

使用dplyr (不是ddplyr )來匯總數據,因此:

require(dplyr)
dxtab = hosp %>% group_by(A,B) %>% 
   summarise(died = sum(Outcome=="Died in Hospital"), n=n(), pc=100*(died/n))

然后ggplot進行救援:

ggplot(dxtab, aes(x=A, y=B, label=sprintf("%2.2f",pc))) + geom_text()

地塊百分比

在摘要中添加stay=mean(LOS)即可獲得該度量。 dxtab看起來像這樣:

   A B died  n        pc      stay
1  1 1    0 12  0.000000  301.5908
2  1 2    0 28  0.000000  301.2882
3  1 3    0 29  0.000000  372.9076
4  1 4    1 23  4.347826  990.2161
5  2 1    0  1  0.000000  244.4200
6  2 2    0 12  0.000000  336.6450
7  2 3    1 15  6.666667  348.7973
8  2 4    4 24 16.666667  638.7075
9  3 1    0  1  0.000000  350.0700
10 3 3    2  4 50.000000  356.1975
11 3 4    2 18 11.111111  905.7672
12 4 3    1  3 33.333333  796.1167
13 4 4    1 14  7.142857 1048.3586
14 5 4   19 38 50.000000  503.0453

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM