---
title: "05 Figures Caries Epidemiology Latvia 2023"
date: 2023-08-15
date-modified: last-modified
date-format: "MMM D, YYYY, HH:mm"
theme: default
format:
# docx: default
# pdf: default
html:
toc: true
toc-location: left
embed-resources: true
toc-expand: 1
code-fold: true
code-tools: true
editor: visual
execute:
echo: false
# cache: true
warning: false
message: false
---
# PACKAGES & DATASETS
```{r}
pacman::p_load(tidyverse,
scales,
gtsummary,
# colorblindr,
patchwork, # for several plots
viridis, # colour palette
janitor,
here)
# remotes::install_github("wilkelab/cowplot")
# remotes::install_github("clauswilke/colorblindr")
library(colorblindr)
```
```{r}
theme_set(theme_minimal())
```
## Colors
```{r}
custom_colors <-
c("#faba39",
"#e4460a",
"#E19896",
# "#7a0403",
"#a2fc3c",
"#1ae4b6",
"#4686fb",
"#30123b")
# Alternative magna
# custom_colors <- c(
# "#fcffa4", # rgb(252, 255, 164)
# "#f7d13d", # rgb(247, 209, 61)
# "#fb9b06", # rgb(251, 155, 6)
# "#ed6925", # rgb(237, 105, 37)
# "#cf4446", # rgb(207, 68, 70)
# "#a52c60", # rgb(165, 44, 96)
# "#781c6d" # rgb(120, 28, 109)
# )
# # alternative viridis
# custom_colors <- c(
# "#fde725", # rgb(253, 231, 37)
# "#b5de2b", # rgb(181, 222, 43)
# "#6ece58", # rgb(110, 206, 88)
# "#35b779", # rgb(53, 183, 121)
# "#1f9e89", # rgb(31, 158, 137)
# "#26828e", # rgb(38, 130, 142)
# "#31688e" # rgb(49, 104, 142)
# )
```
```{r}
# Ver la paleta en diferentes condiciones de daltonismo
# colorblindr::view_cvd(custom_colors)
# Ver la paleta en escala de grises
# colorblindr::view_gray(custom_colors)
```
```{r}
# Extract the colors from the viridis palette
# num_levels <- length(unique(figure_long$index))
# viridis_colors <- viridis(num_levels, option = "H")
# Manually reorder the colors for the levels
# viridis_colors <- viridis(7, option = "H")
# print(viridis_colors)
```
## Dataset for EPi
Load data for Epidemiological Analysis
```{r}
figure <- read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vQFMQYA2P-5LvrP_3jTKdriDvTr7Q8QV_alUzkyIRTjy_M5jhTEsRehau_K9YOfdENES5_FGC9oZKMP/pub?gid=499887873&single=true&output=csv") |>
janitor::clean_names()
```
```{r}
figure <- figure |>
mutate(index = fct_inorder(index))
```
```{r}
figure_long <- figure |>
pivot_longer(cols = d17:d47, names_to = c("delete", "tooth"), names_pattern = "(d)([0-9]+)") |>
select(-delete) |>
mutate(tooth = as.factor(tooth) |>
fct_relevel(c("17", "16", "15", "14", "13", "12", "11",
"21", "22", "23", "24", "25", "26", "27",
"37", "36", "35", "34", "33", "32", "31",
"41", "42", "43", "44", "45", "46", "47")))
```
```{r}
# head(figure)
```
```
```
# Caries
## Fig 12-year-old
```{r}
figure_12_max <- figure_long |>
filter(tooth %in% c("17", "16", "15", "14", "13", "12", "11",
"21", "22", "23", "24", "25", "26", "27")) |>
filter(age == 12) |>
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 1, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
check_overlap = TRUE) + # Avoid text overlap
# scale_fill_viridis_d(direction = -1, option = "H") +
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "12 gadi",
x = "Zobi",
y = "%",
fill = "") +
coord_cartesian(ylim = c(0, 70))
```
```{r}
figure_12_mand <- figure_long |>
filter(tooth %in% c("37", "36", "35", "34", "33", "32", "31",
"41", "42", "43", "44", "45", "46", "47")) |>
filter(age == 12) |>
ggplot(aes(x = tooth,
y = -value, # Multiply value by -1
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 3, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
check_overlap = TRUE) + # Avoid text overlap
scale_fill_manual(values = custom_colors) + # Use the custom colors
# scale_fill_viridis_d(direction = -1, option = "H") +
scale_y_continuous(labels = function(y) scales::percent(abs(y), scale = 1)) + # Add percentage symbol and adjust y-axis labels
labs(title = "",
x = "",
y = "%",
fill = "") +
theme(# axis.title.x = element_blank(), # Remove x-axis title
axis.text.x.top = element_text(angle = 45, hjust = 1), # Move x-axis text to the top and adjust angle
# axis.text.x.bottom = element_blank(), # Remove x-axis text from the bottom
axis.ticks.x.top = element_line(color = "black"), # Add x-axis ticks to the top
legend.position = "none",
axis.ticks.x.bottom = element_blank()) # Remove x-axis ticks from the bottom
```
```{r}
# Combine the two plots
figure_12_max / figure_12_mand
```
```{r}
ggsave(here("figures", "caries_12_plot.png"), device = "png", width = 8.27, height = 11.69, units = "in", dpi = 600)
```
```{r}
# rm(figure,
# figure_12_mand,
# figure_12_max)
```
### Fig 12 all labels
```{r}
figure_12_max <- figure_long |>
filter(tooth %in% c("17", "16", "15", "14", "13", "12", "11",
"21", "22", "23", "24", "25", "26", "27")) |>
filter(age == 12) |>
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = sprintf("%.1f%%", value)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
hjust = 0.5,
vjust = 0.5,
check_overlap = FALSE) + # Show all labels
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "12 gadi",
x = "Zobi",
y = "%",
fill = "") +
coord_cartesian(ylim = c(0, 70))
```
```{r}
figure_12_mand <- figure_long |>
filter(tooth %in% c("37", "36", "35", "34", "33", "32", "31",
"41", "42", "43", "44", "45", "46", "47")) |>
filter(age == 12) |>
ggplot(aes(x = tooth,
y = -value, # Multiply value by -1
fill = index)) +
geom_col() +
geom_text(aes(label = sprintf("%.1f%%", value)),
position = position_stack(vjust = 0.5),
size = 3,
hjust = 0.5,
vjust = 0.5,
check_overlap = FALSE) + # Show all labels
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = function(y) scales::percent(abs(y), scale = 1)) + # Add percentage symbol and adjust y-axis labels
labs(title = "",
x = "",
y = "%",
fill = "") +
theme(axis.text.x.top = element_text(angle = 45, hjust = 1), # Move x-axis text to the top and adjust angle
axis.ticks.x.top = element_line(color = "black"), # Add x-axis ticks to the top
legend.position = "none",
axis.ticks.x.bottom = element_blank()) # Remove x-axis ticks from the bottom
```
```{r}
figure_12_max / figure_12_mand
```
```{r}
ggsave(here("figures", "caries_12_plot_all_labels.png"), device = "png", width = 8.27, height = 11.69, units = "in", dpi = 600)
```
## Figs 15-year-old
```{r}
figure_15_max <- figure_long |>
filter(tooth %in% c("17", "16", "15", "14", "13", "15", "11",
"21", "22", "23", "24", "25", "26", "27")) |>
filter(age == 15) |>
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 1, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
check_overlap = TRUE) + # Avoid text overlap
# scale_fill_viridis_d(direction = -1, option = "H") +
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "15 gadi",
x = "Zobi",
y = "%",
fill = "") +
coord_cartesian(ylim = c(0, 70))
```
```{r}
figure_15_mand <- figure_long |>
filter(tooth %in% c("37", "36", "35", "34", "33", "32", "31",
"41", "42", "43", "44", "45", "46", "47")) |>
filter(age == 15) |>
ggplot(aes(x = tooth,
y = -value, # Multiply value by -1
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 3, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
check_overlap = TRUE) + # Avoid text overlap
scale_fill_manual(values = custom_colors) + # Use the custom colors
# scale_fill_viridis_d(direction = -1, option = "H") +
scale_y_continuous(labels = function(y) scales::percent(abs(y), scale = 1)) + # Add percentage symbol and adjust y-axis labels
labs(title = "",
x = "",
y = "%",
fill = "") +
theme(# axis.title.x = element_blank(), # Remove x-axis title
axis.text.x.top = element_text(angle = 45, hjust = 1), # Move x-axis text to the top and adjust angle
# axis.text.x.bottom = element_blank(), # Remove x-axis text from the bottom
axis.ticks.x.top = element_line(color = "black"), # Add x-axis ticks to the top
legend.position = "none",
axis.ticks.x.bottom = element_blank()) # Remove x-axis ticks from the bottom
```
```{r}
figure_15_max / figure_15_mand
```
```{r}
ggsave(here("figures", "caries_15_plot.png"), device = "png", width = 8.27, height = 11.69, units = "in", dpi = 600)
```
### Fig 15 all labels
```{r}
figure_15_max <- figure_long |>
filter(tooth %in% c("17", "16", "15", "14", "13", "15", "11",
"21", "22", "23", "24", "25", "26", "27")) |>
filter(age == 15) |>
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = sprintf("%.1f%%", value)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
hjust = 0.5,
vjust = 0.5,
check_overlap = FALSE) + # Show all labels
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "15 gadi",
x = "Zobi",
y = "%",
fill = "") +
coord_cartesian(ylim = c(0, 70))
```
```{r}
figure_15_mand <- figure_long |>
filter(tooth %in% c("37", "36", "35", "34", "33", "32", "31",
"41", "42", "43", "44", "45", "46", "47")) |>
filter(age == 15) |>
ggplot(aes(x = tooth,
y = -value, # Multiply value by -1
fill = index)) +
geom_col() +
geom_text(aes(label = sprintf("%.1f%%", value)),
position = position_stack(vjust = 0.5),
size = 3,
hjust = 0.5,
vjust = 0.5,
check_overlap = FALSE) + # Show all labels
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = function(y) scales::percent(abs(y), scale = 1)) + # Add percentage symbol and adjust y-axis labels
labs(title = "",
x = "",
y = "%",
fill = "") +
theme(axis.text.x.top = element_text(angle = 45, hjust = 1), # Move x-axis text to the top and adjust angle
axis.ticks.x.top = element_line(color = "black"), # Add x-axis ticks to the top
legend.position = "none",
axis.ticks.x.bottom = element_blank()) # Remove x-axis ticks from the bottom
```
```{r}
figure_15_max / figure_15_mand
```
```{r}
ggsave(here("figures", "caries_15_plot_all_labels.png"), device = "png", width = 8.27, height = 11.69, units = "in", dpi = 600)
```
```{r}
# rm(figure_15_mand, figure_15_max)
```
# Sealants
```{r}
sealants <- read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vQFMQYA2P-5LvrP_3jTKdriDvTr7Q8QV_alUzkyIRTjy_M5jhTEsRehau_K9YOfdENES5_FGC9oZKMP/pub?gid=447598035&single=true&output=csv") |>
janitor::clean_names()
```
```{r}
sealants <- sealants |>
mutate(index = fct_inorder(index))
```
```{r}
sealants_long <- sealants |>
pivot_longer(cols = d17:d47, names_to = c("delete", "tooth"), names_pattern = "(d)([0-9]+)") |>
select(-delete) |>
mutate(tooth = as.factor(tooth) |>
fct_relevel(c("17", "16", "15", "14", "13", "12", "11",
"21", "22", "23", "24", "25", "26", "27",
"37", "36", "35", "34", "33", "32", "31",
"41", "42", "43", "44", "45", "46", "47")))
```
## silanti 12
```{r}
silanti_12_max <- sealants_long |>
filter(tooth %in% c(17, 16, 15, 14, 24, 25, 26, 27)) |>
filter(age == 12) |>
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 1, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
check_overlap = TRUE) + # Avoid text overlap
# scale_fill_viridis_d(direction = -1, option = "H") +
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "Silanti, 12 gadi",
x = "Zobi",
y = "%",
fill = "") +
coord_cartesian(ylim = c(0, 70))
```
```{r}
silanti_12_mand <- sealants_long %>%
filter(tooth %in% c(37, 36, 35, 34, 44, 45, 46, 47)) %>%
filter(age == 12) %>%
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 1, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
check_overlap = TRUE) + # Avoid text overlap
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "",
x = "",
y = "%",
fill = "") +
coord_cartesian(ylim = c(70, 0)) + # Invert the y-axis
theme(# axis.title.x = element_blank(), # Remove x-axis title
axis.text.x.top = element_text(angle = 45, hjust = 1), # Move x-axis text to the top and adjust angle
axis.ticks.x.top = element_line(color = "black"), # Add x-axis ticks to the top
legend.position = "none",
axis.ticks.x.bottom = element_blank()) # Remove x-axis ticks from the bottom
```
```{r}
silanti_12_max / silanti_12_mand
```
```{r}
ggsave(here("figures", "silanti_12_plot.png"), device = "png", width = 8.27, height = 11.69, units = "in", dpi = 600)
```
## Silanti 15
```{r}
silanti_15_max <- sealants_long |>
filter(tooth %in% c(17, 16, 15, 14, 24, 25, 26, 27)) |>
filter(age == 15) |>
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 1, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
check_overlap = TRUE) + # Avoid text overlap
# scale_fill_viridis_d(direction = -1, option = "H") +
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "Silanti, 15 gadi",
x = "Zobi",
y = "%",
fill = "") +
coord_cartesian(ylim = c(0, 70))
```
```{r}
silanti_15_mand <- sealants_long %>%
filter(tooth %in% c(37, 36, 35, 34, 44, 45, 46, 47)) %>%
filter(age == 15) %>%
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 1, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
check_overlap = TRUE) + # Avoid text overlap
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "",
x = "",
y = "%",
fill = "") +
coord_cartesian(ylim = c(70, 0)) + # Invert the y-axis
theme(# axis.title.x = element_blank(), # Remove x-axis title
axis.text.x.top = element_text(angle = 45, hjust = 1), # Move x-axis text to the top and adjust angle
axis.ticks.x.top = element_line(color = "black"), # Add x-axis ticks to the top
legend.position = "none",
axis.ticks.x.bottom = element_blank()) # Remove x-axis ticks from the bottom
```
```{r}
silanti_15_max / silanti_15_mand
```
```{r}
ggsave(here("figures", "silanti_15_plot.png"), device = "png", width = 8.27, height = 11.69, units = "in", dpi = 600)
```
## Silanti 40%
silanti 12
```{r}
silanti_12_max <- sealants_long |>
filter(tooth %in% c(17, 16, 15, 14, 24, 25, 26, 27)) |>
filter(age == 12) |>
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 1, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
check_overlap = TRUE) + # Avoid text overlap
# scale_fill_viridis_d(direction = -1, option = "H") +
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "Silanti, 12 gadi",
x = "Zobi",
y = "%",
fill = "") +
coord_cartesian(ylim = c(0, 40))
```
```{r}
silanti_12_mand <- sealants_long %>%
filter(tooth %in% c(37, 36, 35, 34, 44, 45, 46, 47)) %>%
filter(age == 12) %>%
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 1, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
check_overlap = TRUE) + # Avoid text overlap
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "",
x = "",
y = "%",
fill = "") +
coord_cartesian(ylim = c(40, 0)) + # Invert the y-axis
theme(# axis.title.x = element_blank(), # Remove x-axis title
axis.text.x.top = element_text(angle = 45, hjust = 1), # Move x-axis text to the top and adjust angle
axis.ticks.x.top = element_line(color = "black"), # Add x-axis ticks to the top
legend.position = "none",
axis.ticks.x.bottom = element_blank()) # Remove x-axis ticks from the bottom
```
```{r}
silanti_12_max / silanti_12_mand
```
```{r}
ggsave(here("figures", "silanti_12_plot_40.png"), device = "png", width = 8.27, height = 11.69, units = "in", dpi = 600)
```
Silanti 15
```{r}
silanti_15_max <- sealants_long |>
filter(tooth %in% c(17, 16, 15, 14, 24, 25, 26, 27)) |>
filter(age == 15) |>
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 1, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
check_overlap = TRUE) + # Avoid text overlap
# scale_fill_viridis_d(direction = -1, option = "H") +
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "Silanti, 15 gadi",
x = "Zobi",
y = "%",
fill = "") +
coord_cartesian(ylim = c(0, 40))
```
```{r}
silanti_15_mand <- sealants_long %>%
filter(tooth %in% c(37, 36, 35, 34, 44, 45, 46, 47)) %>%
filter(age == 15) %>%
ggplot(aes(x = tooth,
y = value,
fill = index)) +
geom_col() +
geom_text(aes(label = ifelse(value > 1, sprintf("%.1f%%", value), NA)),
position = position_stack(vjust = 0.5),
size = 3,
color = "black",
check_overlap = TRUE) + # Avoid text overlap
scale_fill_manual(values = custom_colors) + # Use the custom colors
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Add percentage symbol
labs(title = "",
x = "",
y = "%",
fill = "") +
coord_cartesian(ylim = c(40, 0)) + # Invert the y-axis
theme(# axis.title.x = element_blank(), # Remove x-axis title
axis.text.x.top = element_text(angle = 45, hjust = 1), # Move x-axis text to the top and adjust angle
axis.ticks.x.top = element_line(color = "black"), # Add x-axis ticks to the top
legend.position = "none",
axis.ticks.x.bottom = element_blank()) # Remove x-axis ticks from the bottom
```
```{r}
silanti_15_max / silanti_15_mand
```
```{r}
ggsave(here("figures", "silanti_15_plot_40.png"), device = "png", width = 8.27, height = 11.69, units = "in", dpi = 600)
```
```{r}
# rm(
# sealants,
# sealants_long,
# silanti_12_mand,
# silanti_12_max,
# silanti_15_mand,
# silanti_15_max
# )
```
```{r}
rm(figure_long, custom_colors)
```