Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

r effect estimate plot as error bars by group

My goal is to create a plot showing effect estimates for 4 different levels of a categorical variable X grouped based on a grouping variable M1 like this. This example below has 5 levels for each variable but my X has 4 levels

enter image description here

Suppose this is my test dataset with effect estimates

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

effect_data <- data.frame(
  Level = factor(rep(1:4, each = 12)),  # Assuming 12 sets of analyses
  EffectEstimate = runif(48, min = -2, max = 2),  # Replace with your actual effect estimates
  LowerCI = runif(48, min = -3, max = -1),  # Replace with your actual lower CIs
  UpperCI = runif(48, min = 1, max = 3),  # Replace with your actual upper CIs
  M1  = factor(rep(letters[1:12], times = 4))
)

My plot ends up looking like this

# Plot the effect estimate as a bar chart
ggplot(effect_data, aes(x = Level, y = EffectEstimate)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.7, fill = "blue") +
  geom_errorbar(aes(ymin = LowerCI, ymax = UpperCI), position = position_dodge(0.7), width = 0.25) +
  coord_flip() +
  facet_grid(.~Level)+
  labs(
    title = "Effect Estimates with Confidence Intervals",
    x = "Results from 12 different Models",
    y = "Effect Estimate"
  ) +
  theme_minimal()

This is not what I want, I am trying to generate a plot like the one above , any help is much appreciated. Thanks.

enter image description here

>Solution :

effect_data <- data.frame(
  Level = factor(rep(1:4, each = 12)),
  # Assuming 12 sets of analyses
  EffectEstimate = runif(48, min = -2, max = 2),
  # Replace with your actual effect estimates
  LowerCI = runif(48, min = -3, max = -1),
  # Replace with your actual lower CIs
  UpperCI = runif(48, min = 1, max = 3),
  # Replace with your actual upper CIs
  M1  = factor(rep(letters[1:12], times = 4))
)

From your description I am not sure what you are looking.
Here are two variants. Maybe one of them is what you desire?

library(tidyverse)

# Variant A
ggplot(effect_data,
       aes(
         x = Level,
         y = EffectEstimate,
         group = M1,
         fill = M1
       )) +
  geom_errorbar(aes(ymin = LowerCI, ymax = UpperCI),
                position = position_dodge(0.7),
                width = 0.25) +
  labs(title = "Effect Estimates with Confidence Intervals",
       x = "Results from 12 different Models",
       y = "Effect Estimate") +
  geom_bar(stat = "identity",
           position = "dodge",
           width = 0.7) +
  theme_minimal()


# Variant B
ggplot(effect_data,
       aes(
         x = M1,
         y = EffectEstimate,
         group = Level,
         fill = Level
       )) +
  geom_errorbar(aes(ymin = LowerCI, ymax = UpperCI),
                position = position_dodge(0.7),
                width = 0.25) +
  labs(title = "Effect Estimates with Confidence Intervals",
       x = "Results from 12 different Models",
       y = "Effect Estimate") +
  geom_bar(stat = "identity",
           position = "dodge",
           width = 0.7) +
  theme_minimal()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading