-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5 Figures.R
197 lines (168 loc) · 9.92 KB
/
5 Figures.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Figures showing mixing patterns
library("tidyverse")
library("ggpubr")
library("viridis")
library("gridExtra")
source('3 Analysis.R')
rm(artnet, artnetLong)
mycol4 <- viridis(4)
mycol.ps.full <- c(mycol4[4], mycol4[1:3])
mycol.ps.cc <- c(mycol4[1:3])
mycol.ps.re <- mycol.ps.cc
### Serosorting ----
fig1.panels <- function(scen){
if (scen == "full") {
ss.table <- prop.table(table(artnetSort$hiv3, artnetSort$p_hiv, useNA = "ifany"), 1)
ego <- factor(rep(c("Diagnosed HIV\n", "Test-Negative", "HIV Unknown"), each = 4),
levels = c("Test-Negative", "Diagnosed HIV\n", "HIV Unknown"))
part <- rep(c("Diagnosed HIV", "Test-Negative", "HIV Unknown", "Without Diagnosed HIV"), times = 3)
perc <- c(ss.table[2,2], ss.table[2,1], ss.table[2,3], 0,
ss.table[1,2], ss.table[1,1], ss.table[1,3], 0,
ss.table[3,2], ss.table[3,1], ss.table[3,3], 0)
panel.title <- "\nEgo Knowledge"}
if (scen == "cc") {
cc.df <- artnetSort %>% filter(!p_hiv == "Unk", !hiv3 == "Unk")
ss.table <- prop.table(table(cc.df$hiv3, cc.df$p_hiv, useNA = "ifany"), 1)
ego <- factor(rep(c("Diagnosed HIV\n", "Test-Negative"), each = 4),
levels = c("Test-Negative", "Diagnosed HIV\n"))
part <- rep(c("Diagnosed HIV", "Test-Negative", "HIV Unknown", "Without Diagnosed HIV"), times = 2)
perc <- c(ss.table[2,2], ss.table[2,1], 0, 0,
ss.table[1,2], ss.table[1,1], 0, 0)
panel.title <- "\nComplete-Case Analysis"}
if (scen == "reclass") {
ss.table <- as.data.frame(results(dat = reclass.results, x = "hh.sort.p"))
ego <- factor(rep(c("Diagnosed HIV", "Without\nDiagnosed HIV"), each = 4),
levels = c("Without\nDiagnosed HIV", "Diagnosed HIV"))
part <- rep(c("Diagnosed HIV", "Test-Negative", "HIV Unknown", "Without Diagnosed HIV"), times = 2)
perc <- c(ss.table[4,2], 0, 0, ss.table[2,2],
ss.table[3,2], 0, 0, ss.table[1,2])
panel.title <- "\nReclassification Analysis"}
ss.df <- data.frame(ego, part, perc)
bar.order <- c("HIV Unknown", "Test-Negative", "Without Diagnosed HIV", "Diagnosed HIV")
fig1.panel <- ggplot(ss.df,
aes(x = ego, y = perc,
fill = factor(part, levels = bar.order))) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = scales::percent,
expand = c(0.01,0)) +
scale_x_discrete(expand = c(0,0)) +
geom_label(data = subset(ss.df, perc > 0),
aes(label = scales::percent(perc, accuracy = 0.1)),
position = position_stack(vjust = 0.5), size = 5.5,
show.legend = FALSE, fill = "white") +
labs(fill = "Alter HIV Status",
title = panel.title,
x = "") +
theme(plot.title = element_text(hjust = 0.5, size = 14),
axis.title = element_blank(),
axis.ticks.x = element_blank(),
axis.text = element_text(size = 12,
face = "bold",
color = "black"),
text = element_text(size = 12,
face = "bold",
color = "black"),
strip.text = element_text(size = 12,
face = "bold",
color = "black"),
legend.position = "right",
legend.text = element_text(size = 12,
face = "bold",
color = "black"),
strip.background = element_blank(),
panel.background = element_blank()) +
scale_fill_manual(values = c("grey28", mycol4[2], mycol4[1], mycol4[3]))
#scale_fill_manual(values = c("black", "grey28", "grey77", "grey52"))
if (scen == "full") {ss.full <<- fig1.panel}
if (scen == "cc") {ss.cc <<- fig1.panel}
if (scen == "reclass") {ss.reclass <<- fig1.panel}
}
fig1.panels(scen = "full")
fig1.panels(scen = "cc")
fig1.panels(scen = "reclass")
## Combined plots
(ss.compare <- ggarrange(ss.full, ss.cc, ss.reclass,
ncol = 3, widths = c(1,0.7,0.7),
labels = c("A","B","C"),
common.legend = TRUE,
legend = "bottom"))
## PrEP Sorting ----
fig2.panels <- function(scen){
if (scen == "full") {
ps.table <- prop.table(table(artnetSort$hp, artnetSort$p_hp),1)
ego <- factor(rep(c("Diagnosed\nHIV", "Never PrEP", "PrEP", "HIV\nUnknown"), each = 4),
levels = c("Never PrEP", "PrEP", "Diagnosed\nHIV", "HIV\nUnknown"))
part <- rep(c("Diagnosed HIV", "Never PrEP", "Ever PrEP", "Unknown PrEP"), times = 4)
perc <- c(ps.table[2,2], ps.table[2,1], ps.table[2,3], ps.table[2,4],
ps.table[1,2], ps.table[1,1], ps.table[1,3], ps.table[1,4],
ps.table[3,2], ps.table[3,1], ps.table[3,3], ps.table[3,4],
ps.table[4,2], ps.table[4,1], ps.table[4,3], ps.table[4,4])
panel.title <- "\nEgo Knowledge"}
if (scen == "cc") {
cc.df <- artnetSort %>% filter(!p_hiv == "Unk" & !prep.during.part2 == "Unk" & !hiv3 == "Unk")
ps.table <- prop.table(table(cc.df$hp, cc.df$p_hp),1)
ego <- factor(rep(c("Diagnosed\nHIV", "Never PrEP", "PrEP"), each = 4),
levels = c("Never PrEP", "PrEP", "Diagnosed\nHIV"))
part <- rep(c("Diagnosed HIV", "Never PrEP", "Ever PrEP", "Unknown PrEP"), times = 3)
perc <- c(ps.table[2,2], ps.table[2,1], ps.table[2,3], 0,
ps.table[1,2], ps.table[1,1], ps.table[1,3], 0,
ps.table[3,2], ps.table[3,1], ps.table[3,3], 0)
panel.title <- "\nComplete-Case Analysis"}
if (scen == "reclass") {
ps.table <- as.data.frame(results(dat = reclass.results, x = "full.sort.p"))
ego <- factor(rep(c("Diagnosed\nHIV", "Never PrEP", "PrEP"), each = 4),
levels = c("Never PrEP", "PrEP", "Diagnosed\nHIV"))
part <- rep(c("Diagnosed HIV", "Never PrEP", "Ever PrEP", "Unknown PrEP"), times = 3)
perc <- c(ps.table[5,2], ps.table[2,2], ps.table[8,2], 0,
ps.table[4,2], ps.table[1,2], ps.table[7,2], 0,
ps.table[6,2], ps.table[3,2], ps.table[9,2], 0)
panel.title <- "\nReclassification Analysis"}
ps.df <- data.frame(ego, part, perc)
bar.order <- c("Unknown PrEP", "Ever PrEP", "Never PrEP", "Diagnosed HIV")
fig2.panel <- ggplot(ps.df,
aes(x = ego, y = perc,
fill = factor(part, levels = bar.order))) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = scales::percent,
expand = c(0.01,0)) +
scale_x_discrete(expand = c(0,0)) +
geom_label(data = subset(ps.df, perc > 0),
aes(label = scales::percent(perc, accuracy = 0.1)),
position = position_stack(vjust = 0.5), size = 5.5,
show.legend = FALSE, fill = "white") +
labs(fill = "Alter HIV Status and PrEP Use",
title = panel.title,
x = "") +
theme(plot.title = element_text(hjust = 0.5, size = 14),
axis.title = element_blank(),
axis.ticks.x = element_blank(),
axis.text = element_text(size = 12,
face = "bold",
color = "black"),
text = element_text(size = 12,
face = "bold",
color = "black"),
strip.text = element_text(size = 12,
face = "bold",
color = "black"),
legend.position = "right",
legend.text = element_text(size = 12,
face = "bold",
color = "black"),
strip.background = element_blank(),
panel.background = element_blank()) +
scale_fill_manual(values = c("grey28", mycol4[2], mycol4[1], mycol4[3]))
#scale_fill_manual(values = c("black", "grey28", "grey77", "grey52"))
if (scen == "full") {ps.full <<- fig2.panel}
if (scen == "cc") {ps.cc <<- fig2.panel}
if (scen == "reclass") {ps.reclass <<- fig2.panel}
}
fig2.panels(scen = "full")
fig2.panels(scen = "cc")
fig2.panels(scen = "reclass")
## Combined plots
(ps.compare <- ggarrange(ps.full, ps.cc, ps.reclass,
ncol = 3, widths = c(4,3.2,3.2),
labels = c("A","B","C"),
common.legend = TRUE,
legend = "bottom"))