I am trying to create a forest plot to compare estimates of different models. Tho goal is to plot all the estimates of the same predictor for the different models so they are comparable.
For this I have written the following plotting function:
# Create the combined_data dataframe
# My data looks something like this
combined_data <- data.frame(
Parameter = c("x1", "x2", "x1", "x2", "x3"),
Coefficient = c(1.78358873, -1.14376132, 1.84360374, -1.22307433, -0.08670885),
SE = c(0.1070057, 0.1010112, 0.1620157, 0.1982852, 0.1845809),
CI = c(0.95, 0.95, 0.95, 0.95, 0.95),
CI_low = c(1.5712121, -1.3442406, 1.5220889, -1.6166156, -0.4530508),
CI_high = c(1.9959654, -0.9432820, 2.1651185, -0.8295330, 0.2796331),
p = c(3.151569e-30, 1.923809e-19, 1.269702e-19, 1.591506e-08, 6.395803e-01),
Model = c("Model 1", "Model 1", "Model 2", "Model 3", "Model 3")
)
# Display the dataframe
print(combined_data)
# ploitng function
plot_model_coefficients <- function(combined_data, plot_title = "Coefficient Plot",
y_axis_label = "Parameters", x_axis_label = "Coefficient",
dodge = position_dodge(width = 0.6)) {
# Choose colors based on the number of models
num_models <- length(unique(combined_data$Model)) # Count of unique models
point_colors <- RColorBrewer::brewer.pal(n = num_models, name = "Set2")
# Create the plot
p <- ggplot(combined_data, aes(x = Parameter, y = Coefficient, color = Model, group = Model)) +
geom_point(size = 3, position = dodge) + # Points for the Coefficient
geom_errorbar(aes(ymin = CI_low, ymax = CI_high, color = Model),
width = 0.2, position = dodge) + # Error bars for CI
geom_hline(yintercept = 0, color = "black", linetype = "dashed", size = 1) + # Horizontal line at y=0
theme_minimal() + # Minimal theme
labs(title = plot_title, x = x_axis_label, y = y_axis_label) +
scale_color_manual(values = point_colors) + # Custom color for points
coord_flip() + # Flip coordinates for better visibility
theme(legend.title = element_blank())
# Print the plot
print(p)
}
plot_model_coefficients(combined_data,
plot_title = "Stacked Coefficients Plot",
y_axis_label = "Estimate",
x_axis_label = "Predictors")
This creats a plot looking like so:
However I'd like to create a plot where I can add some information e.g. the CI for every estimate like in this plot:
I tried working with geom_text() but I was only able to add the information directly over the estimates and I dont no how I can create this table like tructure nex to the estimates. I also looked into the forestploterpackage in Rhowever I couldn't figure out how I can group the estimates when the estimates were used in mutiple models.



geom_textrecognises thexandyaesthetics, so just pass it the coordinates at which you want the text to appear, probably withh_justas well. You'll probably need to do that with a data set other thancombined_data. Also, I suggest returning the plot from your function, not printing it from within the function. That makes the function more general.geom_text()to add a text over the single elemts. I dont think you can use it to create a seperat colum to the right of the plot aligned with the estimates unless you are wlling to edit every singel position of the CI text seperately.yco-ordinate of the labels. Thexco-ordinate should be constant. As always, @allancameron's contribution is accurate and informative.geom_text(). Thanks!