# Tạo dataframe đầy đủ với p-value
forest_data <- data.frame(
  ten_bien = c(
    "Đau ngực",
    "Dạng hang",
    "5cm ≤ tổn thương ≤ 7cm",
    "Nốt mờ",
    "Thùy trên trái",
    "Vị trí sinh thiết"
  ),
  OR = c(
    4.95,
    0.06,
    5.47,
    0.23,
    3.52,
    9.30
  ),
  lowCI = c(
    1.62,
    0.002,
    1.56,
    0.05,
    1.20,
    3.05
  ),
  highCI = c(
    17.36,
    0.47,
    22.24,
    0.91,
    11.28,
    33.34
  ),
  p = c(
    0.007,
    0.019,
    0.011,
    0.047,
    0.026,
    0.0001
  )
)
#Vẽ Forest plot 
# Cài nếu chưa có
#install.packages("ggplot2")

library(ggplot2)

forest_data$lowCI_plot <- ifelse(forest_data$OR < 1, forest_data$OR / 2, forest_data$lowCI)
forest_data$highCI_plot <- ifelse(forest_data$OR < 1, forest_data$OR * 2, forest_data$highCI)
forest_data$hjust_pos <- ifelse(forest_data$OR < 1, 1.1, -0.2)
forest_data$label_ci <- paste0(
  "OR = ", round(forest_data$OR, 2),
  " [", round(forest_data$lowCI, 2),
  " – ", round(forest_data$highCI, 2), "]"
)
library(ggthemes)
ggplot(forest_data, aes(x = reorder(ten_bien, OR), y = OR)) +
  geom_point(size = 3, shape = 18, color = "blue") +
  geom_errorbar(aes(ymin = lowCI_plot, ymax = highCI_plot), width = 0.2, color = "blue") +
  geom_hline(yintercept = 1, linetype = "dashed", color = "red", linewidth = 1) +
  scale_y_log10(breaks = c(0.01, 0.1, 1, 10, 100), limits = c(0.01, 50)) +
  coord_flip() +
  theme_minimal(base_size = 13) +
  labs(
    x = NULL,
    y = "Odds Ratio",
    title = "Forest Plot"
  ) +
  geom_text(
    aes(label = label_ci),
    vjust = -1.2,   # Đẩy lên trên điểm
    size =2.5,
    color = "black"
  ) + theme_economist()+ theme(plot.title = element_text(hjust = 0.5))

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