library(readxl)
library(ggplot2)

# 读取 IV_LA sheet
df_raw <- read_excel(
  "C:/Users/JUNSONG SHI/OneDrive/Data/FDI&Inequality/summary data/figures.xlsx",
  sheet = "IV_LA"
)

# 只保留有系数的行
df <- df_raw[!is.na(df_raw$coef), ]

df$sample <- as.character(df$sample)
df$n1 <- as.numeric(df$n1)

# Panel 标题位置
panel_labels <- data.frame(
  panel_label = c(
    "Panel A: LAC with IVs",
    "Panel B: LAC with lagged IVs"
  ),
  y = c(-1, -5),
  x = -0.010
)

p <- ggplot(df, aes(y = n1)) +
  
  # 0 线
  geom_vline(
    xintercept = 0,
    color = "firebrick",
    linetype = "dashed",
    linewidth = 0.8
  ) +
  
  # 95% CI 横线
  geom_segment(
    aes(x = low, xend = up, yend = n1),
    color = "gray25",
    linetype = "dashed",
    linewidth = 0.8
  ) +
  
  # CI 左端点
  geom_segment(
    aes(x = low, xend = low, y = n1 - 0.15, yend = n1 + 0.15),
    color = "gray25",
    linewidth = 0.8
  ) +
  
  # CI 右端点
  geom_segment(
    aes(x = up, xend = up, y = n1 - 0.15, yend = n1 + 0.15),
    color = "gray25",
    linewidth = 0.8
  ) +
  
  # 系数点
  geom_point(
    aes(x = coef),
    color = "#1f77b4",
    size = 3
  ) +
  
  # 右侧变量名标签
  geom_text(
    aes(x = up + 0.001, label = sample),
    hjust = 0,
    size = 4
  ) +
  
  # Panel 标题
  geom_text(
    data = panel_labels,
    aes(x = x, y = y, label = panel_label),
    inherit.aes = FALSE,
    hjust = 0,
    size = 4.5
  ) +
  
  scale_x_continuous(
    limits = c(-0.015, 0.105),
    breaks = seq(-0.01, 0.09, 0.02)
  ) +
  
  scale_y_continuous(
    limits = c(-9.5, 0),
    breaks = NULL
  ) +
  
  labs(
    x = "Coef. and 95% CIs in different samples",
    y = NULL
  ) +
  
  theme_classic(base_size = 13) +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.line.y = element_blank(),
    axis.line.x = element_line(color = "black", linewidth = 0.8),
    axis.title.x = element_text(size = 13),
    plot.margin = margin(10, 35, 10, 10)
  )

p

ggsave(
  "IV_LA_coef_plot.tif",
  plot = p,
  width = 9,
  height = 6,
  dpi = 300
)