Created
March 24, 2024 03:42
-
-
Save paulovillarroel/2848c11dd7372b5c21fb6aef8d4cd2c0 to your computer and use it in GitHub Desktop.
Graph on hospital discharges of malignant neoplasms in Chile (2022)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(ggdist) | |
library(glue) | |
library(ggtext) | |
#url <- "https://www.fonasa.cl/sites/fonasa/datos-abiertos/bases-grd" | |
# Unzip file | |
unzip("raw-data/2022.zip", exdir = "raw-data/") | |
# Delete zip file | |
file.remove("raw-data/2022.zip") | |
# Save file into a variable | |
grd_2022 <- read_delim("raw-data/GRD_PUBLICO_EXTERNO_2022.txt", | |
delim = "|", | |
locale = locale(encoding = "UTF-16LE") | |
) | |
# Read ICE-10 codes | |
icd_10 <- openxlsx::read.xlsx("src/CIE-10.xlsx") |> | |
janitor::clean_names() | |
# Join with ICD-10 codes | |
grd_2022 <- grd_2022 |> | |
janitor::clean_names() |> | |
left_join(icd_10, by = c("diagnostico1" = "codigo")) | |
# Discharge malignant neoplasm cases | |
icd10_malignant_neoplasms <- c( | |
"c0[0-9].*?|c0[0-9].*?", | |
"c1[0-9].*?|c1[0-9].*?", | |
"c2[0-9].*?|c2[0-9].*?", | |
"c3[0-9].*?|c3[0-9].*?", | |
"c4[0-9].*?|c4[0-9].*?", | |
"c5[0-9].*?|c5[0-9].*?", | |
"c6[0-9].*?|c6[0-9].*?", | |
"c7[0-9].*?|c7[0-9].*?", | |
"c8[0-9].*?|c8[0-9].*?", | |
"c9[0-9].*?|c9[0-9].*?" | |
) | |
malignant_neoplasms <- grd_2022 |> | |
janitor::clean_names() |> | |
filter( | |
tipo_actividad == "HOSPITALIZACIÓN", | |
str_detect(str_to_lower(categoria), paste(icd10_malignant_neoplasms, collapse = "|")) | |
) |> | |
mutate(estancia = as.Date(fechaalta) - as.Date(fecha_ingreso)) |> | |
select(diagnostico1, descripcion, categoria, estancia) | |
# Plot | |
mean_stay <- mean(malignant_neoplasms$estancia) | |
median_stay <- median(malignant_neoplasms$estancia) | |
n_discharges <- nrow(malignant_neoplasms) | |
n_discharges_colon <- malignant_neoplasms |> | |
filter(categoria == "C18 TUMOR MALIGNO DEL COLON") |> | |
nrow() | |
median_stay <- median(malignant_neoplasms$estancia) | |
bg_color <- "grey90" | |
plot_subtitle <- glue("En base a datos disponibles de egresos hospitalarios GRD 2022. La base fue filtradas | |
para los casos de neoplasias malignas (código CIE-10: C18). | |
Total de egresos por neoplasias malignas: {n_discharges}, de las cuales {n_discharges_colon} corresponden a tumores malignos de colon. | |
Se muestra la distribución de los días de estancias por topografía.") | |
p <- malignant_neoplasms |> | |
filter(categoria == "C18 TUMOR MALIGNO DEL COLON") |> | |
group_by(descripcion) |> | |
mutate( | |
discharge_total = n(), | |
mean_stay = mean(estancia), | |
median_stay = median(estancia), | |
descripcion = str_remove(descripcion, "Tumor maligno del "), | |
descripcion = str_to_upper(descripcion) | |
) |> | |
ungroup() |> | |
mutate(descripcion = fct_reorder(descripcion, -median_stay)) |> | |
ggplot(aes(descripcion, estancia)) + | |
ggdist::stat_halfeye(fill_type = "segments", alpha = 0.5, ) + | |
ggdist::stat_interval() + | |
stat_summary(geom = "point", fun = median) + | |
geom_hline(yintercept = median_stay, col = "grey30", lty = "dashed") + | |
scale_y_continuous(breaks = seq(0, 70, 10)) + | |
annotate("text", | |
y = median_stay + 10, x = 11.5, label = "Mediana estancia (neoplasias malignas)", | |
size = 3, vjust = 0 | |
) + | |
scale_color_manual( | |
values = MetBrewer::met.brewer("Monet"), | |
labels = c( | |
"0.95" = "95% de las estancias", | |
"0.8" = "80% de las estancias", | |
"0.5" = "50% de las estancias" | |
) | |
) + | |
coord_flip(ylim = c(0, 70), clip = "off") + | |
labs( | |
title = toupper("Estancias hospitalarias por neoplasias malignas de colon"), | |
subtitle = plot_subtitle, | |
caption = "Eje acortado a 70 días de estancia hospitalaria | |
Data: FONASA | Datos abiertos | |
Egresos hospitalarios base GRD 2022 - Chile | |
Extraído desde https://www.fonasa.cl/sites/fonasa/datos-abiertos/bases-grd | |
Visualización: Paulo Villarroel", | |
x = NULL, | |
y = "Estancia (días)", | |
color = "Distribución:" | |
) + | |
theme_minimal() + | |
theme( | |
legend.position = "bottom", | |
plot.background = element_rect(color = NA, fill = bg_color), | |
panel.grid = element_blank(), | |
panel.grid.major.x = element_line(size = 0.1, color = "grey75"), | |
plot.title.position = "plot", | |
plot.title = element_text(size = 17, margin = margin(b = 10), face = "bold"), | |
plot.subtitle = element_textbox_simple(margin = margin(t = 4, b = 16), size = 13), | |
plot.caption.position = "plot", | |
axis.text.y = element_text(hjust = 1, margin = margin(l = 10, r = -2), face = "bold"), | |
plot.margin = margin(4, 4, 4, 4) | |
) | |
p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment