-
Notifications
You must be signed in to change notification settings - Fork 0
/
bar_chart_each_ubs.R
281 lines (238 loc) · 10.2 KB
/
bar_chart_each_ubs.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
## Install required packages
# List of required packages
pacotes <- c("plotly", "tidyverse", "kableExtra", "ggplot2", "tidyr", "dplyr",
"lubridate", "stringr", "gridExtra", "grid")
# Check if any of the required packages are not installed
if(sum(as.numeric(!pacotes %in% installed.packages())) != 0){
# If not installed, get the names of the packages to install
instalador <- pacotes[!pacotes %in% installed.packages()]
# Install the missing packages
for(i in 1:length(instalador)) {
install.packages(instalador, dependencies = TRUE)
break() # Break the loop after installing the first package
}
# Load all required packages
sapply(pacotes, require, character.only = TRUE)
} else {
# If all packages are already installed, just load them
sapply(pacotes, require, character.only = TRUE)
}
### Set working directory where files will be saved and loaded
setwd("/home/gabriela/Documentos/REDE_influenza/SOLICITACOES/IGOR/Figuras_PSP/")
# Verify and display the current working directory
getwd()
# Load the data from a CSV file
d1 <- read.csv(file = "BASE_UPAS_Taina_31012024_APENAS_2023.csv", header = TRUE, sep = ';')
# Select relevant columns from the data
d_sel <- select(d1, UBS, RESULT_INF_A, RESULT_INF_B, RESULT_COV)
# Create a list of specific UBS names
ubs_list <- c("JACANA", "BUTANTAN", "MARIA ANTONIETA", "TATUAPE", "TITO LOPES", "VERGUEIRO")
##################FLUA Analysis#########################################
# Function to process data for a specific UBS
processar_ubs <- function(df, ubs_nome, col_resultado) {
df %>%
filter(UBS == ubs_nome) %>%
group_by({{col_resultado}}) %>%
summarise(COUNT = n())
}
# Loop through each UBS in the list to get FLUA results
resultados_UBS <- lapply(ubs_list, function(ubs_nome) {
processar_ubs(d_sel, ubs_nome, RESULT_INF_A)
})
# Assign results to named objects
names(resultados_UBS) <- paste0("contagem_A_", ubs_list)
# Assign individual results to named objects in the global environment (optional)
list2env(resultados_UBS, envir = .GlobalEnv)
# Remove rows with no information from the FLUA results
contagem_A_JACANA <- contagem_A_JACANA %>% slice(-1)
`contagem_A_MARIA ANTONIETA` <- `contagem_A_MARIA ANTONIETA` %>% slice(-1)
contagem_A_TATUAPE <- contagem_A_TATUAPE %>% slice(-1)
`contagem_A_TITO LOPES` <- `contagem_A_TITO LOPES` %>% slice(-1)
contagem_A_VERGUEIRO <- contagem_A_VERGUEIRO %>% slice(-1)
# Modify the data frames for FLUA results
contagem_A_JACANA <- contagem_A_JACANA %>%
mutate(VIRUS = case_when(
RESULT_INF_A == "DETECTABLE" ~ "FLUA",
RESULT_INF_A == "INCONCLUSIVE" ~ "FLUA",
RESULT_INF_A == "NOT-DETECTABLE" ~ "FLUA"
)) %>%
select(RESULT = RESULT_INF_A, COUNT, VIRUS)
# Repeat the modification for other UBS data frames
contagem_A_BUTANTAN <- contagem_A_BUTANTAN %>%
mutate(VIRUS = case_when(
RESULT_INF_A == "DETECTABLE" ~ "FLUA",
RESULT_INF_A == "INCONCLUSIVE" ~ "FLUA",
RESULT_INF_A == "NOT-DETECTABLE" ~ "FLUA"
)) %>%
select(RESULT = RESULT_INF_A, COUNT, VIRUS)
`contagem_A_MARIA ANTONIETA` <- `contagem_A_MARIA ANTONIETA` %>%
mutate(VIRUS = case_when(
RESULT_INF_A == "DETECTABLE" ~ "FLUA",
RESULT_INF_A == "INCONCLUSIVE" ~ "FLUA",
RESULT_INF_A == "NOT-DETECTABLE" ~ "FLUA"
)) %>%
select(RESULT = RESULT_INF_A, COUNT, VIRUS)
contagem_A_TATUAPE <- contagem_A_TATUAPE %>%
mutate(VIRUS = case_when(
RESULT_INF_A == "DETECTABLE" ~ "FLUA",
RESULT_INF_A == "INCONCLUSIVE" ~ "FLUA",
RESULT_INF_A == "NOT-DETECTABLE" ~ "FLUA"
)) %>%
select(RESULT = RESULT_INF_A, COUNT, VIRUS)
`contagem_A_TITO LOPES` <- `contagem_A_TITO LOPES` %>%
mutate(VIRUS = case_when(
RESULT_INF_A == "DETECTABLE" ~ "FLUA",
RESULT_INF_A == "INCONCLUSIVE" ~ "FLUA",
RESULT_INF_A == "NOT-DETECTABLE" ~ "FLUA"
)) %>%
select(RESULT = RESULT_INF_A, COUNT, VIRUS)
contagem_A_VERGUEIRO <- contagem_A_VERGUEIRO %>%
mutate(VIRUS = case_when(
RESULT_INF_A == "DETECTABLE" ~ "FLUA",
RESULT_INF_A == "INCONCLUSIVE" ~ "FLUA",
RESULT_INF_A == "NOT-DETECTABLE" ~ "FLUA"
)) %>%
select(RESULT = RESULT_INF_A, COUNT, VIRUS)
##################FLUB Analysis#########################################
# Loop through each UBS in the list to get FLUB results
resultados_UBS_B <- lapply(ubs_list, function(ubs_nome) {
processar_ubs(d_sel, ubs_nome, RESULT_INF_B)
})
# Assign results to named objects
names(resultados_UBS_B) <- paste0("contagem_B_", ubs_list)
# Assign individual results to named objects in the global environment (optional)
list2env(resultados_UBS_B, envir = .GlobalEnv)
# Remove rows with no information from the FLUB results
contagem_B_JACANA <- contagem_B_JACANA %>% slice(-1)
`contagem_B_MARIA ANTONIETA` <- `contagem_B_MARIA ANTONIETA` %>% slice(-1)
contagem_B_TATUAPE <- contagem_B_TATUAPE %>% slice(-1)
`contagem_B_TITO LOPES` <- `contagem_B_TITO LOPES` %>% slice(-1)
contagem_B_VERGUEIRO <- contagem_B_VERGUEIRO %>% slice(-1)
# Modify the data frames for FLUB results
contagem_B_JACANA <- contagem_B_JACANA %>%
mutate(VIRUS = case_when(
RESULT_INF_B == "DETECTABLE" ~ "FLUB",
RESULT_INF_B == "INCONCLUSIVE" ~ "FLUB",
RESULT_INF_B == "NOT-DETECTABLE" ~ "FLUB"
)) %>%
select(RESULT = RESULT_INF_B, COUNT, VIRUS)
# Repeat the modification for other UBS data frames
contagem_B_BUTANTAN <- contagem_B_BUTANTAN %>%
mutate(VIRUS = case_when(
RESULT_INF_B == "DETECTABLE" ~ "FLUB",
RESULT_INF_B == "INCONCLUSIVE" ~ "FLUB",
RESULT_INF_B == "NOT-DETECTABLE" ~ "FLUB"
)) %>%
select(RESULT = RESULT_INF_B, COUNT, VIRUS)
`contagem_B_MARIA ANTONIETA` <- `contagem_B_MARIA ANTONIETA` %>%
mutate(VIRUS = case_when(
RESULT_INF_B == "DETECTABLE" ~ "FLUB",
RESULT_INF_B == "INCONCLUSIVE" ~ "FLUB",
RESULT_INF_B == "NOT-DETECTABLE" ~ "FLUB"
)) %>%
select(RESULT = RESULT_INF_B, COUNT, VIRUS)
contagem_B_TATUAPE <- contagem_B_TATUAPE %>%
mutate(VIRUS = case_when(
RESULT_INF_B == "DETECTABLE" ~ "FLUB",
RESULT_INF_B == "INCONCLUSIVE" ~ "FLUB",
RESULT_INF_B == "NOT-DETECTABLE" ~ "FLUB"
)) %>%
select(RESULT = RESULT_INF_B, COUNT, VIRUS)
`contagem_B_TITO LOPES` <- `contagem_B_TITO LOPES` %>%
mutate(VIRUS = case_when(
RESULT_INF_B == "DETECTABLE" ~ "FLUB",
RESULT_INF_B == "INCONCLUSIVE" ~ "FLUB",
RESULT_INF_B == "NOT-DETECTABLE" ~ "FLUB"
)) %>%
select(RESULT = RESULT_INF_B, COUNT, VIRUS)
contagem_B_VERGUEIRO <- contagem_B_VERGUEIRO %>%
mutate(VIRUS = case_when(
RESULT_INF_B == "DETECTABLE" ~ "FLUB",
RESULT_INF_B == "INCONCLUSIVE" ~ "FLUB",
RESULT_INF_B == "NOT-DETECTABLE" ~ "FLUB"
)) %>%
select(RESULT = RESULT_INF_B, COUNT, VIRUS)
##################COVID Analysis#########################################
# Loop through each UBS in the list to get COVID results
resultados_UBS_C <- lapply(ubs_list, function(ubs_nome) {
processar_ubs(d_sel, ubs_nome, RESULT_COV)
})
# Assign results to named objects
names(resultados_UBS_C) <- paste0("contagem_C_", ubs_list)
# Assign individual results to named objects in the global environment (optional)
list2env(resultados_UBS_C, envir = .GlobalEnv)
# Remove rows with no information from the COVID results
contagem_C_JACANA <- contagem_C_JACANA %>% slice(-1)
`contagem_C_MARIA ANTONIETA` <- `contagem_C_MARIA ANTONIETA` %>% slice(-1)
contagem_C_TATUAPE <- contagem_C_TATUAPE %>% slice(-1)
`contagem_C_TITO LOPES` <- `contagem_C_TITO LOPES` %>% slice(-1)
contagem_C_VERGUEIRO <- contagem_C_VERGUEIRO %>% slice(-1)
# Modify the data frames for COVID results
contagem_C_JACANA <- contagem_C_JACANA %>%
mutate(VIRUS = case_when(
RESULT_COV == "DETECTABLE" ~ "COVID",
RESULT_COV == "INCONCLUSIVE" ~ "COVID",
RESULT_COV == "NOT-DETECTABLE" ~ "COVID"
)) %>%
select(RESULT = RESULT_COV, COUNT, VIRUS)
# Repeat the modification for other UBS data frames
contagem_C_BUTANTAN <- contagem_C_BUTANTAN %>%
mutate(VIRUS = case_when(
RESULT_COV == "DETECTABLE" ~ "COVID",
RESULT_COV == "INCONCLUSIVE" ~ "COVID",
RESULT_COV == "NOT-DETECTABLE" ~ "COVID"
)) %>%
select(RESULT = RESULT_COV, COUNT, VIRUS)
`contagem_C_MARIA ANTONIETA` <- `contagem_C_MARIA ANTONIETA` %>%
mutate(VIRUS = case_when(
RESULT_COV == "DETECTABLE" ~ "COVID",
RESULT_COV == "INCONCLUSIVE" ~ "COVID",
RESULT_COV == "NOT-DETECTABLE" ~ "COVID"
)) %>%
select(RESULT = RESULT_COV, COUNT, VIRUS)
contagem_C_TATUAPE <- contagem_C_TATUAPE %>%
mutate(VIRUS = case_when(
RESULT_COV == "DETECTABLE" ~ "COVID",
RESULT_COV == "INCONCLUSIVE" ~ "COVID",
RESULT_COV == "NOT-DETECTABLE" ~ "COVID"
)) %>%
select(RESULT = RESULT_COV, COUNT, VIRUS)
`contagem_C_TITO LOPES` <- `contagem_C_TITO LOPES` %>%
mutate(VIRUS = case_when(
RESULT_COV == "DETECTABLE" ~ "COVID",
RESULT_COV == "INCONCLUSIVE" ~ "COVID",
RESULT_COV == "NOT-DETECTABLE" ~ "COVID"
)) %>%
select(RESULT = RESULT_COV, COUNT, VIRUS)
contagem_C_VERGUEIRO <- contagem_C_VERGUEIRO %>%
mutate(VIRUS = case_when(
RESULT_COV == "DETECTABLE" ~ "COVID",
RESULT_COV == "INCONCLUSIVE" ~ "COVID",
RESULT_COV == "NOT-DETECTABLE" ~ "COVID"
)) %>%
select(RESULT = RESULT_COV, COUNT, VIRUS)
##################Combine and Plot Data################################
# Combine all the individual results into one data frame for plotting
d_total <- bind_rows(
contagem_A_JACANA, contagem_A_BUTANTAN, `contagem_A_MARIA ANTONIETA`,
contagem_A_TATUAPE, `contagem_A_TITO LOPES`, contagem_A_VERGUEIRO,
contagem_B_JACANA, contagem_B_BUTANTAN, `contagem_B_MARIA ANTONIETA`,
contagem_B_TATUAPE, `contagem_B_TITO LOPES`, contagem_B_VERGUEIRO,
contagem_C_JACANA, contagem_C_BUTANTAN, `contagem_C_MARIA ANTONIETA`,
contagem_C_TATUAPE, `contagem_C_TITO LOPES`, contagem_C_VERGUEIRO
)
# Ensure 'UBS' and 'RESULT' columns are of character type for plotting
d_total$UBS <- as.character(d_total$UBS)
d_total$RESULT <- as.character(d_total$RESULT)
# Set up the plotting theme
theme_set(theme_minimal())
# Plot the data using ggplot
ggplot(d_total, aes(x = VIRUS, y = COUNT, fill = VIRUS)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~ UBS) +
labs(x = "Virus", y = "Count", fill = "Virus",
title = "Virus Detection Counts by UBS") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_fill_manual(values = c("FLUA" = "blue", "FLUB" = "red", "COVID" = "green"))
# Save the plot to a file
ggsave("virus_detection_counts_by_ubs.png", width = 12, height = 8)