lung_data <- data.frame(
Variable = c("Dyspnea", "Lower airway obstruction syndrome", "Respiratory infection syndrome",
"Mediastinal lymph nodes", "Pleural invasion", "Mediastinal invasion",
"Endobronchial tumor lesion", "Left upper lobe bronchus (bronchoscopy)",
"Biopsy site"),
OddsRatio = c(4.058, 1.634, 0.456, 2.071, 6.469, 0.61, 1.686, 6.217, 4.309),
LowerCI = c(1.294, 0.36, 0.12, 0.672, 1.49, 0.148, 0.36, 1.91, 1.452),
UpperCI = c(12.724, 7.419, 17.35, 6.382, 28.086, 2.509, 7.892, 20.239, 12.783),
Significance = c("Yes", "No", "No", "No", "Yes", "No", "No", "Yes", "Yes")
)
# Hiển thị dữ liệu
print(lung_data)
## Variable OddsRatio LowerCI UpperCI
## 1 Dyspnea 4.058 1.294 12.724
## 2 Lower airway obstruction syndrome 1.634 0.360 7.419
## 3 Respiratory infection syndrome 0.456 0.120 17.350
## 4 Mediastinal lymph nodes 2.071 0.672 6.382
## 5 Pleural invasion 6.469 1.490 28.086
## 6 Mediastinal invasion 0.610 0.148 2.509
## 7 Endobronchial tumor lesion 1.686 0.360 7.892
## 8 Left upper lobe bronchus (bronchoscopy) 6.217 1.910 20.239
## 9 Biopsy site 4.309 1.452 12.783
## Significance
## 1 Yes
## 2 No
## 3 No
## 4 No
## 5 Yes
## 6 No
## 7 No
## 8 Yes
## 9 Yes
# Cài đặt thư viện nếu chưa có
#install.packages("ggplot2")
#install.packages("ggtext") # Hỗ trợ hiển thị text đẹp hơn
library(ggplot2)
library(ggtext)
library(ggthemes)
# Nhập dữ liệu
lung_data <- data.frame(
Variable = c("Pleural invasion", "Left upper lobe bronchus (bronchoscopy)",
"Biopsy site", "Dyspnea", "Mediastinal lymph nodes",
"Endobronchial tumor lesion", "Lower airway obstruction syndrome",
"Mediastinal invasion", "Respiratory infection syndrome"),
OddsRatio = c(6.469, 6.217, 4.309, 4.058, 2.071, 1.686, 1.634, 0.61, 0.456),
LowerCI = c(1.49, 1.91, 1.452, 1.294, 0.672, 0.36, 0.36, 0.148, 0.12),
UpperCI = c(28.086, 20.239, 12.783, 12.724, 6.382, 7.892, 7.419, 2.509, 1.735),
Significance = c("Yes", "Yes", "Yes", "Yes", "No", "No", "No", "No", "No")
)
# Sắp xếp: Biến có ý nghĩa lên trên, xếp theo Odds Ratio từ cao đến thấp
lung_data$color<- factor(lung_data$Variable,
levels = c(
lung_data$Variable[lung_data$Significance == "Yes"][order(-lung_data$OddsRatio[lung_data$Significance == "Yes"])],
lung_data$Variable[lung_data$Significance == "No"]
))
# Màu sắc: Xanh cho biến có ý nghĩa, Xám nhạt cho biến không có ý nghĩa
lung_data$Color <- ifelse(lung_data$Significance == "Yes", "blue", "lightgray")
lung_data$Shape <- ifelse(lung_data$Significance == "Yes", 16, 17)
# Nhập dữ liệu
# Cài đặt thư viện nếu chưa có
#install.packages("ggplot2")
#install.packages("ggtext")
library(ggplot2)
library(ggtext)
lung_data <- data.frame(
Variable = c("Pleural invasion", "Left upper lobe bronchus (bronchoscopy)",
"Biopsy site", "Dyspnea", "Mediastinal lymph nodes",
"Endobronchial tumor lesion", "Lower airway obstruction syndrome",
"Mediastinal invasion", "Respiratory infection syndrome"),
OddsRatio = c(6.469, 6.217, 4.309, 4.058, 2.071, 1.686, 1.634, 0.61, 0.456),
LowerCI = c(1.49, 1.91, 1.452, 1.294, 0.672, 0.36, 0.36, 0.148, 0.12),
UpperCI = c(28.086, 20.239, 12.783, 12.724, 6.382, 7.892, 7.419, 2.509, 1.735),
Significance = c("Yes", "Yes", "Yes", "Yes", "No", "No", "No", "No", "No")
)
# Chuyển Significance thành số: "Yes" = 1, "No" = 0
lung_data$SignificanceNumeric <- ifelse(lung_data$Significance == "Yes", 1, 0)
# Sắp xếp: Biến có ý nghĩa lên trên (Significance = 1), theo OR giảm dần
lung_data <- lung_data[order(-lung_data$SignificanceNumeric, -lung_data$OddsRatio), ]
# Chuyển Variable thành factor để ggplot vẽ đúng thứ tự
lung_data$Variable <- factor(lung_data$Variable, levels = rev(lung_data$Variable))
# Ký hiệu hình dạng: Tròn (●) cho biến có ý nghĩa, Tam giác (▲) cho biến không có ý nghĩa
lung_data$Shape <- ifelse(lung_data$Significance == "Yes", 17, 16) # 16 = circle, 17 = triangle
# Vẽ Forest Plot
ggplot(lung_data, aes(x = Variable, y = OddsRatio, ymin = LowerCI, ymax = UpperCI)) +
geom_pointrange(aes(shape = as.factor(Shape), color = Significance), size = 0.6) + # Ký hiệu hình dạng
scale_shape_manual(values = c("16" = 16, "17" = 17)) + # Tròn = 16, Tam giác = 17
scale_color_manual(values = c("Yes" = "blue", "No" = "gray")) + # Màu xanh đậm cho Yes, xám cho No
geom_hline(yintercept = 1, linetype = "dashed", color = "red", linewidth = 1) + # Đường tham chiếu OR = 1
geom_text(aes(label = paste0("OR = ", round(OddsRatio, 2))),
vjust = -1, size = 3, color = "gray") + # Hiển thị OR phía trên mỗi điểm
coord_flip() + # Xoay trục
labs(title = "Forest Plot of Odds Ratio",
x = "",
y = "Odds Ratio (95% CI)") +
theme_economist() +
theme(legend.position = "none")

ggsave("forest_plot.png", width = 8, height = 6, dpi = 300)