# 解析結果のデータフレーム作成
analysis_results <- data.frame(
  Method = c("IPTW (main analysis)", "Propensity score matching", "Antibiotics duration 2 days or more", "Antibiotics duration 3 days or more", "Antibiotics duration 4 days or more", "Third-generation cephalosporins only"),
  OR = c(1.11, 1.12, 1.14, 1.05, 0.96, 1.57),
  Lower_CI = c(0.61, 0.62, 0.59, 0.50, 0.43, 0.64),
  Upper_CI = c(1.99, 2.03, 2.20, 2.21, 2.18, 3.87),
  P_value = c(0.74, 0.71, 0.70, 0.91, 0.93, 0.33),
  Group = c("Group 1", "Group 1", "Group 2", "Group 2", "Group 2", "Group 1")
)

# フォレストプロットの作成
library(ggplot2)

forest_plot <- ggplot(analysis_results, aes(x = reorder(Method, -OR), y = OR, ymin = Lower_CI, ymax = Upper_CI)) +
  geom_pointrange(size = 0.5) +
  geom_hline(yintercept = 1, linetype = "dashed") +
  scale_y_log10() +
  coord_flip() +theme_bw() +
labs(
x = "Analysis Method",
y = "Odds Ratio (95% Confidence Interval)"
) +
geom_text(aes(label = sprintf("p = %.2f", P_value)), vjust = -1.0, size = 3) + #"p = %.2f" ここの2fの数値が小数点のまるめ
scale_x_discrete(limits = rev(analysis_results$Method), expand = c(0, 0.5))
 
#プロットの表示
print(forest_plot)

forest_plot <- ggplot(analysis_results, aes(x = reorder(Method, -OR), y = OR, ymin = Lower_CI, ymax = Upper_CI)) +
  geom_pointrange(size = 0.5) +
  geom_hline(yintercept = 1, linetype = "dashed") +
  scale_y_continuous(breaks = seq(0, 5, 0.5), limits = c(0, 4)) + # 対数スケールを使用しないで等間隔のスケールを使用
  coord_flip() +
  theme_bw() +
  labs(
    x = "Analysis Method",
    y = "Odds Ratio (95% Confidence Interval)"
  ) +
  geom_text(aes(label = sprintf("p = %.2f", P_value)), vjust = -1.0, size = 3) +
  scale_x_discrete(limits = rev(analysis_results$Method), expand = c(0, 0.5))
 
print(forest_plot)