Visualisation các chỉ số đuối nước

format: html: embed-resources: true

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

pacman::p_load(
  tidyverse, ggplot2, labelled, Hmisc, sjmisc, sf, haven,
  hrbrthemes, viridis, patchwork, heatmaply, readxl, DT, summarytools 
)
Installing package into 'C:/Users/Phuong Mai/AppData/Local/R/win-library/4.5'
(as 'lib' is unspecified)
Warning: package 'hrbrthemes' is not available for this version of R

A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.5:
  cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.5/PACKAGES'
Warning: 'BiocManager' not available.  Could not check Bioconductor.

Please use `install.packages('BiocManager')` and then retry.
Warning in p_install(package, character.only = TRUE, ...):
Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
logical.return = TRUE, : there is no package called 'hrbrthemes'
Warning in pacman::p_load(tidyverse, ggplot2, labelled, Hmisc, sjmisc, sf, : Failed to install/load:
hrbrthemes
tysuat <- read_excel("Ty_suat.xlsx", sheet = "Sheet1")

ggplot(tysuat, aes(x = reorder(`Nguyên nhân`, TS_Tổng), y = TS_Tổng)) +
  geom_col(fill = "steelblue", color = "black") +
  
  # Thêm nhãn số liệu
  geom_text(aes(label = sprintf("%.2f", TS_Tổng)), hjust = -0.2, size = 3.5) +
  
  # Lật ngang biểu đồ
  coord_flip() +
  
  # Đặt tên cho tiêu đề và các trục
  labs(
    title = "Tỷ suất tử vong trẻ em và vị thành niên do tai nạn thương tích năm 2022",
    y = "Tỷ suất /100.000 trẻ em và vị thành niên",
    x = "Nguyên nhân"  # Ở đây dùng chuỗi ký tự bình thường là đúng
  ) +
  
  # Tùy chỉnh theme
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    panel.background = element_blank(), 
    axis.line = element_line(color = "black")
  )

# Đọc dữ liệu
so_truonghop <- read_excel("So_truong_hop.xlsx", sheet = "Sheet1")

# Vẽ biểu đồ cột ngang
ggplot(so_truonghop, aes(x = reorder(`Nguyên nhân`, TH_Tổng), y = TH_Tổng)) +
  geom_col(fill = "steelblue", color = "black") +
  
  # Thêm nhãn số liệu trên cột
  geom_text(aes(label = sprintf("%d", TH_Tổng)), hjust = -0.2, size = 3.5) +
  
  # Lật ngang biểu đồ
  coord_flip() +
  
  # Đặt tiêu đề và nhãn trục
  labs(
    title = "Tổng số trẻ tử vong theo nguyên nhân",
    y = "Số trẻ tử vong (0-19 tuổi)",
    x = "Nguyên nhân"
  ) +
  
  # Tùy chỉnh giao diện
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    panel.background = element_blank(),
    axis.line = element_line(color = "black")
  )

# Đọc dữ liệu
so_truonghop <- read_excel("So_truong_hop.xlsx", sheet = "Sheet1")

# Tạo biến mới: TH_0_9 = TH_0_4 + TH_5_9
so_truonghop <- so_truonghop %>%
  mutate(TH_0_9 = TH_0_4 + TH_5_9)

# Vẽ biểu đồ cột ngang
ggplot(so_truonghop, aes(x = reorder(`Nguyên nhân`, TH_0_9), y = TH_0_9)) +
  geom_col(fill = "steelblue", color = "black") +
  
  # Thêm nhãn số liệu nhỏ trên cột
  geom_text(aes(label = sprintf("%d", TH_0_9)), hjust = -0.2, size = 3) +
  
  # Lật ngang biểu đồ
  coord_flip() +
  
  # Đặt tiêu đề và nhãn trục
  labs(
    title = "Nguyên nhân tử vong của trẻ em (0-9 tuổi)",
    subtitle = "Do tai nạn thương tích theo nhóm tuổi năm 2022",
    y = "Số trẻ tử vong",
    x = "Nguyên nhân"
  ) +
  
  # Tùy chỉnh giao diện
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    plot.subtitle = element_text(size = 11, face = "italic"),
    panel.background = element_blank(),
    axis.line = element_line(color = "black")
  )

# Đọc dữ liệu
so_truonghop <- read_excel("So_truong_hop.xlsx", sheet = "Sheet1")

# Tạo biến mới: TH_10_19 = TH_10_14 + TH_15_19
so_truonghop <- so_truonghop %>%
  mutate(TH_10_19 = TH_10_14 + TH_15_19)

# Vẽ biểu đồ cột ngang
ggplot(so_truonghop, aes(x = reorder(`Nguyên nhân`, TH_10_19), y = TH_10_19)) +
  geom_col(fill = "steelblue", color = "black") +
  
  # Thêm nhãn số liệu nhỏ trên cột
  geom_text(aes(label = sprintf("%d", TH_10_19)), hjust = -0.2, size = 3) +
  
  # Lật ngang biểu đồ
  coord_flip() +
  
  # Đặt tiêu đề và nhãn trục
  labs(
    title = "Nguyên nhân tử vong của trẻ vị thành niên (10-19 tuổi)",
    subtitle = "Do tai nạn thương tích theo nhóm tuổi năm 2022",
    y = "Số trẻ vị thành niên tử vong",
    x = "Nguyên nhân"
  ) +
  
  # Tùy chỉnh giao diện
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    plot.subtitle = element_text(size = 11, face = "italic"),
    panel.background = element_blank(),
    axis.line = element_line(color = "black")
  )

# Đọc dữ liệu
so_vung <- read_excel("So_truong_hop_vung.xlsx", sheet = "Sheet1")

# Chuyển dữ liệu từ dạng rộng sang dạng dài (để ggplot vẽ stack được)
so_vung_long <- so_vung %>%
  pivot_longer(cols = c("TH_Nam", "TH_Nu"),
               names_to = "Gioi_tinh",
               values_to = "So_truong_hop")

# Đặt nhãn đẹp hơn
so_vung_long <- so_vung_long %>%
  mutate(Gioi_tinh = recode(Gioi_tinh,
                            "TH_Nam" = "Nam",
                            "TH_Nu" = "Nữ"))

# Vẽ biểu đồ cột ngang xếp chồng
ggplot(so_vung_long, aes(x = reorder(Vung, So_truong_hop, sum), 
                         y = So_truong_hop, 
                         fill = Gioi_tinh)) +
  geom_col(position = "stack", color = "black") +
  
  # Lật ngang biểu đồ
  coord_flip() +
  
  # Đặt tiêu đề, trục, và chú giải
  labs(
    title = "Số trường hợp tử vong do đuối nước theo vùng",
    x = "Vùng",
    y = "Số trường hợp tử vong",
    fill = "Giới tính"
  ) +
  
  # Màu tương ứng với Stata: Nam = navy, Nữ = lightblue
  scale_fill_manual(values = c("Nam" = "navy", "Nữ" = "lightblue")) +
  
  # Tùy chỉnh giao diện
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    panel.background = element_blank(),
    axis.line = element_line(color = "black"),
    legend.title = element_text(size = 11),
    legend.text = element_text(size = 10)
  )

# Đọc dữ liệu
ty_suat_vung <- read_excel("Ty_suat_vung.xlsx", sheet = "Sheet1")

# Chuyển dữ liệu từ dạng rộng sang dài để ggplot vẽ stacked bar chart
ty_suat_vung_long <- ty_suat_vung %>%
  pivot_longer(cols = c("TS_Nam", "TS_Nu"),
               names_to = "Gioi_tinh",
               values_to = "Ty_suat")

# Gắn nhãn giới tính cho đẹp
ty_suat_vung_long <- ty_suat_vung_long %>%
  mutate(Gioi_tinh = recode(Gioi_tinh,
                            "TS_Nam" = "Nam",
                            "TS_Nu" = "Nữ"))

# Vẽ biểu đồ cột ngang xếp chồng
ggplot(ty_suat_vung_long, aes(x = reorder(Vung, Ty_suat, sum),
                              y = Ty_suat,
                              fill = Gioi_tinh)) +
  geom_col(position = "stack", color = "black") +
  
  # Lật ngang biểu đồ
  coord_flip() +
  
  # Đặt tiêu đề, trục, và chú giải
  labs(
    title = "Tỷ suất tử vong do đuối nước theo vùng và giới tính",
    x = "Vùng",
    y = "Tỷ suất tử vong (trên 100.000 trẻ em và vị thành niên)",
    fill = "Giới tính"
  ) +
  
  # Màu tương ứng với Stata: Nam = navy, Nữ = lightblue
  scale_fill_manual(values = c("Nam" = "navy", "Nữ" = "lightblue")) +
  
  # Tùy chỉnh giao diện
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    panel.background = element_blank(),
    axis.line = element_line(color = "black"),
    legend.title = element_text(size = 11),
    legend.text = element_text(size = 10)
  )

# Đọc dữ liệu
ty_suat_vung <- read_excel("Ty_suat_vung.xlsx", sheet = "Sheet1")

# Chuyển dữ liệu từ dạng rộng sang dạng dài để ggplot vẽ cột nhóm
ty_suat_vung_long <- ty_suat_vung %>%
  pivot_longer(cols = c("TS_Nam", "TS_Nu"),
               names_to = "Gioi_tinh",
               values_to = "Ty_suat") %>%
  mutate(Gioi_tinh = recode(Gioi_tinh,
                            "TS_Nam" = "Nam",
                            "TS_Nu" = "Nữ"))

# Vẽ biểu đồ cột ngang nhóm theo giới tính
ggplot(ty_suat_vung_long, aes(x = reorder(Vung, Ty_suat, sum),
                              y = Ty_suat,
                              fill = Gioi_tinh)) +
  geom_col(position = position_dodge(width = 0.8), color = "black") +
  
  # Thêm nhãn số liệu lên mỗi thanh
  geom_text(aes(label = sprintf("%.2f", Ty_suat)),
            position = position_dodge(width = 0.8),
            hjust = -0.2, size = 3) +
  
  # Lật ngang biểu đồ
  coord_flip() +
  
  # Đặt tiêu đề, trục, và chú giải
  labs(
    title = "Tỷ suất tử vong của TE & TVN nam và nữ theo vùng",
    x = "Vùng",
    y = "Tỷ suất tử vong (trên 100.000 trẻ em và vị thành niên)",
    fill = "Giới tính"
  ) +
  
  # Màu sắc tương ứng Stata
  scale_fill_manual(values = c("Nam" = "navy", "Nữ" = "lightblue")) +
  
  # Tùy chỉnh theme
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    panel.background = element_blank(),
    axis.line = element_line(color = "black"),
    legend.title = element_text(size = 11),
    legend.text = element_text(size = 10)
  )

# Đọc dữ liệu
ty_suat_vung <- read_excel("Ty_suat_vung.xlsx", sheet = "Sheet1")

# Vẽ biểu đồ cột ngang đơn giản (1 biến)
ggplot(ty_suat_vung, aes(x = reorder(Vung, TS_Chung), y = TS_Chung)) +
  geom_col(fill = "navy", color = "black") +
  
  # Thêm nhãn số liệu
  geom_text(aes(label = sprintf("%.2f", TS_Chung)),
            hjust = -0.2, size = 3.5) +
  
  # Lật ngang biểu đồ
  coord_flip() +
  
  # Đặt tiêu đề, trục
  labs(
    title = "Tỷ suất tử vong do đuối nước theo vùng sinh thái",
    x = "Vùng sinh thái",
    y = "Tỷ suất tử vong (trên 100.000 trẻ em và vị thành niên)"
  ) +
  
  # Tùy chỉnh giao diện
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    panel.background = element_blank(),
    axis.line = element_line(color = "black")
  )