-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathf - limit_of_detection_bar_plots.R
339 lines (294 loc) · 16.1 KB
/
f - limit_of_detection_bar_plots.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
################################## Graph The Chemical Measurements By LOD ###################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Purpose: This function creates and saves barplot and scatterplot of LOD info per chemical
#
# Inputs: subset_chemicals - dataframe of chemicals that passed the LOD check
#
# Outputs: barplot and scatterplot
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
limit_of_detection_bar_plots <- function(subset_chemicals)
{
library(tidyverse)
library(ggplot2)
library(dplyr)
library(ggrepel)
library(colorspace)
library(ggpubr)
setwd(current_directory)
#temporary
# subset_chemicals <- use_these_chems
#############################################################################################################
#################################### Make a Below Column in subset_chemicals ################################
#############################################################################################################
#make below column and move it after above column - relocate() doesn't work
subset_chemicals_above_below <- subset_chemicals %>%
mutate(below = total_number_people_unweighted - above_num_people_unweighted)
subset_chemicals_above_below <- subset_chemicals_above_below %>%
dplyr::select(chemical_codename_use,
chemical_name,
chem_family,
above_num_people_unweighted,
below,
total_number_people_unweighted,
above_percentage_unweighted)
#remove the units from subset_chemicals for scatterplot later
#this drops the units from the chemical names
subset_chemicals$chemical_name <- gsub("\\s\\(([^()]+)\\)$"
, ""
, subset_chemicals$chemical_name)
#############################################################################################################
############################################## Make a Long Dataset ##########################################
#############################################################################################################
long_subset_chemicals <- gather(data = subset_chemicals_above_below, #this is the wide dataset
key = over_under, #this is the new column to describe the number
value = count, #these are the counts per chemical
above_num_people_unweighted:below #these are the columns to adjust
)
# Define a vector of chemical family names in a particular order
chem_family_levels <- c("Acrylamide"
# , "Melamine"
, "Brominated Flame Retardants (BFR)"
, "Phosphate Flame Retardants (PFR)"
, "Polychlorinated Biphenyls (PCB)"
, "Dioxins"
, "Furans"
, "Metals"
, "Phthalates & Plasticizers"
, "Personal Care & Consumer Product Compounds"
, "Pesticides"
, "Aromatic Amines"
# , "Phytoestrogens"
, "Polyaromatic Hydrocarbons (PAH)"
, "Volatile Organic Compounds (VOC)"
, "Smoking Related Compounds"
, "Per- and Polyfluoroalkyl Substances (PFAS)"
, "Aldehydes"
# , "Dietary Components"
, "Other")
# Ensure that the levels of the chemical family are in a defined order to ensure proper color scheme
long_subset_chemicals$chem_family <- factor(long_subset_chemicals$chem_family
, levels = chem_family_levels)
#############################################################################################################
##################################### Define Colors for Each Chemical Class #################################
#############################################################################################################
# Define a string vector of color hexcodes for the chemical family in corresponding order
light_colors <- c()
dark_colors <- c("#8B0000" # Acrylamide
# , "#9b870c" # Melamine
, "#EE0000" # BFRs
, "#FF6B00" # PFRs
, "#FF69B4" # PCBs
, "#FFA500" # Dioxins
, "#EEEE00" # Furans
, "#228B22" # Metals
, "#A4D3EE" # Phthalates & Plasticizers
, "#A2CD5A" # Personal Care
, "#1E90FF" # Pesticides
, "#be67c9" # Aromatic Amines
# , "#7D26CD" # Phytoestrogens
, "#cf9b76" # PAHs
, "#828282" # VOCs
, "#8B4513" # Smoking
, "#FFB6C1" # PFCs
, "#0E1171" # Aldehydes
, "#BABABA" ) # Other
light_colors <- lighten(dark_colors, amount = 0.6)
# Concatenate light and dark colors together
mycolors <- c(dark_colors, light_colors)
# This drops the units from the chemical names
long_subset_chemicals$chemical_name <- gsub("\\s\\(([^()]+)\\)$"
, ""
, long_subset_chemicals$chemical_name)
# Shorten this terrible name
long_subset_chemicals$chemical_name <- gsub("N-Acetyl-S-(2-hydroxy-3-methyl-3-butenyl)-L-cysteine + N-Acetyl-S-(2-hydroxy-2-methyl-3-butenyl)-L-cysteine",
"N-Acetyl-S-(2-hydroxy-2/3-methyl-3-butenyl)-L-cysteine",
long_subset_chemicals$chemical_name,
fixed = TRUE)
#############################################################################################################
######################################### Make The Barplot By Chemical ######################################
#############################################################################################################
chem_by_partic_by_LOD <-
ggplot(data = long_subset_chemicals,
aes(x = count,
y = reorder(chemical_name, count)))+
geom_col(aes(fill = interaction(chem_family, over_under)), width = 0.6,
position = position_stack(reverse = TRUE))+
labs(x = "Participants", y = "")+
scale_x_continuous(expand = c(0,0))+
scale_fill_manual(values = mycolors,
name = "Limit of Detection")+
theme(axis.text.y = element_text(colour = "black",
# angle = 90,
vjust = 0.5, hjust=1,
size = 4),
axis.text.x = element_text(size = 10),
axis.title = element_text(size = 10)) +
theme(panel.background = element_rect(fill = "white", #this is the background
colour = "black", #this is the border
linewidth = 0.1, linetype = "solid"))+
theme(panel.grid.major.x = element_line(color = "grey"),
panel.grid.minor.x = element_line(color = "grey",
linetype = "dashed"))+
theme(legend.position = "none")+
theme(plot.margin = margin(0.2, #top
6, #right
0.2, #bottom
0.1, #left
"cm"))
################################################ Make a Legend ##############################################
subset_chemicals_above_below$chem_family <- factor(subset_chemicals_above_below$chem_family
, levels = chem_family_levels)
legend_plot <-
ggplot(data=subset_chemicals_above_below,
aes(x=chemical_name,
y=total_number_people_unweighted,
fill = chem_family)) +
geom_bar(stat="identity") +
scale_fill_manual("Chemical Family",
values=dark_colors)
legend <- as_ggplot(get_legend(legend_plot))
################################################## Save Plot ################################################
setwd(paste0(current_directory, "/Bar Plots - Sample Size by Toxicant"))
# Save the plot and legend as a pdf for viewing at a high resolution
print("chemicals by LOD numbers_weighted_color.pdf")
ggsave(filename = "chemicals by LOD numbers_weighted_color.pdf"
, plot = chem_by_partic_by_LOD
, width = 9
, height = 9)
# Save the plot and legend as a png for presentation
print("chemicals by LOD numbers_weighted_color.png")
ggsave(filename = "chemicals by LOD numbers_weighted_color.png"
, plot = chem_by_partic_by_LOD
, units = "in"
, width = 9
, height = 9
, dpi = 500)
ggsave(filename = "chemicals by LOD numbers_weighted_color_legend.png"
, plot = legend
, units = "in"
, width = 3
, height = 5
, dpi = 500)
setwd(current_directory)
#############################################################################################################
####################################### Make The Scatterplot By Chemical ####################################
#############################################################################################################
# Define a vector of chemical family names in a particular order
chem_family_levels <- c("Acrylamide"
# , "Melamine"
, "Brominated Flame Retardants (BFR)"
, "Phosphate Flame Retardants (PFR)"
, "Polychlorinated Biphenyls (PCB)"
, "Dioxins"
, "Furans"
, "Metals"
, "Phthalates & Plasticizers"
, "Personal Care & Consumer Product Compounds"
, "Pesticides"
, "Aromatic Amines"
# , "Phytoestrogens"
, "Polyaromatic Hydrocarbons (PAH)"
, "Volatile Organic Compounds (VOC)"
, "Smoking Related Compounds"
, "Per- and Polyfluoroalkyl Substances (PFAS)"
, "Aldehydes"
# , "Dietary Components"
, "Other")
# Redefine the column vector containing the chemical family as a factor with the levels
subset_chemicals$chem_family <- factor(subset_chemicals$chem_family
, levels = chem_family_levels)
# Define a string vector of color hexcodes for the chemical family in corresponding order
chem_family_colors <- c("#8B0000" # Acrylamide
# , "#9b870c" # Melamine
, "#EE0000" # BFRs
, "#FF6B00" # PFRs
, "#FF69B4" # PCBs
, "#FFA500" # Dioxins
, "#EEEE00" # Furans
, "#228B22" # Metals
, "#A4D3EE" # Phthalates & Plasticizers
, "#A2CD5A" # Personal Care
, "#1E90FF" # Pesticides
, "#be67c9" # Aromatic Amines
# , "#7D26CD" # Phytoestrogens
, "#cf9b76" # PAHs
, "#828282" # VOCs
, "#8B4513" # Smoking
, "#FFB6C1" # PFCs
, "#0E1171" # Aldehydes
# , "#49FA0F" # Dietary Components
, "#BABABA" ) # Other
# Define a string vector of shape codes for the chemical family in corresponding order
chem_family_shapes <- c(16 # Acrylamide
# , "#9b870c" # Melamine
, 16 # BFRs
, 16 # PFRs
, 16 # PCBs
, 16 # Dioxins
, 16 # Furans
, 18 # Metals
, 16 # Phthalates & Plasticizers
, 16 # Personal Care
, 16 # Pesticides
, 17 # Aromatic Amines
# , 16 # Phytoestrogens
, 16 # PAHs
, 15 # VOCs
, 16 # Smoking
, 16 # PFCs
, 16 # Aldehydes
, 25 ) # Other
#Scatterplot of # participants vs. % over LOD
scatter_partic_by_chem_LOD <-
ggplot(subset_chemicals,
aes(x = total_number_people_unweighted,
y = above_percentage_unweighted,
color = chem_family,
shape = chem_family))+
geom_point(size = 2)+
scale_color_manual(name = "Chemical Family",
values = chem_family_colors) +
scale_shape_manual(name = "Chemical Family",
values = chem_family_shapes)+
geom_text(aes(label=ifelse(total_number_people_unweighted > 30000, as.character(chemical_name),'')),
vjust = -1,
size = 4,
show.legend = FALSE)+
geom_text_repel(aes(label=ifelse(above_percentage_unweighted < 55 & total_number_people_unweighted < 20000,
as.character(chemical_name),'')),
hjust = -0.1,
size = 4,
show.legend = FALSE)+
labs(x = "Participants per Chemical",
y = "Measurements Above LOD (%)")+
theme(axis.text.x = element_text(colour = "black",
size = 15),
axis.text.y = element_text(colour = "black",
size = 15))+
theme(axis.title = element_text(size = 20))+
theme(legend.text = element_text(size = 15),
legend.title = element_text(size = 20))+
guides(colour = guide_legend(override.aes = list(size=5)))+
theme(legend.key = element_rect(fill = NA, color = NA))+
theme(panel.background = element_rect(fill = "white",
colour = "black", #this is the border
size = 0.1, linetype = "solid"))
setwd(paste0(current_directory, "/Bar Plots - Sample Size by Toxicant"))
# Save the plot as a pdf for viewing at a high resolution
print("participants and percent over LOD_weighted.pdf")
ggsave(filename = "participants and percent over LOD_weighted.pdf"
, plot = scatter_partic_by_chem_LOD
, width = 14
, height = 9)
# Save the plot as a png for presentation
print("participants and percent over LOD_weighted.png")
ggsave(filename = "participants and percent over LOD_weighted.png"
, plot = scatter_partic_by_chem_LOD
, units = "in"
, width = 14
, height = 9
, dpi = 300)
#############################################################################################################
setwd(current_directory)
}