krtbris
krtbris

Reputation: 368

Annotating a forest plot in ggplot outside of the facets

I am generating a forest plot using ggplot2 in R and want to add a label at the top of the plot to indicate the direction of effect of hazard ratios. However, when I do this it adds the labels to each facet rather than just the top of the plot. I have tried using draw_plot_label also but this needs coordinates rather than x-axis values which I don't want because I am generating dozens of versions of this plot and need the labels to be consistent every time regardless of what is on the plot.

library(ggplot2)

# Sample data
final_cox_dat <- data.frame(
  comparator = rep(c("Group A", "Group B", "Group C"), each = 2),
  estimate = c(1.2, 1.5, 0.8, 0.9, 1.4, 1.7),
  conf.low = c(1.0, 1.3, 0.7, 0.8, 1.3, 1.5),
  conf.high = c(1.5, 1.7, 0.9, 1.0, 1.6, 1.9),
  model = rep(c("Weighted", "Unweighted"), times = 3)
)

# Create a forest plot using ggplot2 
forest_plot <-
  ggplot(
    final_cox_dat,
    aes(
      x = estimate, y = comparator,
      xmin = conf.low, xmax = conf.high, 
      color = model
    )
  ) +
  geom_pointrange(position = position_dodge(width = 0.8)) +
  geom_vline(xintercept = 1, linetype = "dashed") +
  facet_wrap(~comparator, ncol = 1, scales = "free_y") +
  labs(
    title = NULL,
    x = "Hazard Ratio (95% CI)",
    y = NULL
  ) +
  theme_minimal() +
  theme(
    strip.text = element_blank(), # Hide both x and y-axis labels in facet
    legend.title = element_blank(),
    legend.position = "bottom",
    axis.title.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_color_manual(
    values = c("Unweighted" = "red", "Weighted" = "blue")
  ) +
  geom_text(
    aes(
      x = max(conf.high) + 0.05,
      group = model,
      label = sprintf(
        "%0.2f (%0.2f, %0.2f)",
        estimate, conf.low, conf.high
      )
    ),
    hjust = 0, vjust = 0.5, size = 4.5, color = "black",
    position = position_dodge(width = 0.8)
  ) +
  scale_x_continuous(breaks = seq(0, max(final_cox_dat$conf.high + 0.2), by = 0.2), expand = expansion(mult = 0.1)) +
  theme(text = element_text(size = 18))  +
  guides(color = guide_legend(reverse=TRUE))

# Annotate plot with direction of hazard ratio 

forest_plot <-  forest_plot + annotate("text", x = 1, y = Inf, label = "Favours Drug", vjust = 1, hjust = 1.5, color = "black", size = 6) +
  annotate("text", x = 1, y = Inf, label = "Favours comparator", vjust = 1, hjust = -0.5, color = "black", size = 6) 

enter image description here

Upvotes: 0

Views: 297

Answers (1)

stefan
stefan

Reputation: 123903

The issue is that annotate does not know about your facets. Instead you could use a geom_text which via the data= argument allows to specify the facet where you want to add your annotation:

library(ggplot2)

forest_plot +
  geom_text(
    data = data.frame(
      x = c(.9, 1.1), y = Inf, comparator = "Group A",
      label = c("Favours Drug", "Favours comparator"),
      hjust = c(1, 0)
    ),
    aes(x = x, y = y, label = label, hjust = hjust),
    vjust = 1, color = "black", size = 6,
    inherit.aes = FALSE
  )

enter image description here

Upvotes: 1

Related Questions