Skip to content

Instantly share code, notes, and snippets.

@avallecam
Created November 1, 2024 20:53
Show Gist options
  • Save avallecam/cbf112382681396a80aa04b90ef9515d to your computer and use it in GitHub Desktop.
Save avallecam/cbf112382681396a80aa04b90ef9515d to your computer and use it in GitHub Desktop.
create a dot whiskers plot with raw coeficient values, after passing one sample figure to chatgpt to generate a reprex
# Load libraries
library(tidyverse)

# Create data for the simple regression model (Panel A)
simple_data <- data.frame(
  Predictor = "Aware that humans can get TBD",
  Estimate = 0.3,
  SE = 0.1
)

# Create data for the multiple regression model (Panel B)
multiple_data <- data.frame(
  Predictor = c(
    "Aware that humans can get TBD", "Female", "44-59", "60-80",
    "High school", "Technical education", "University",
    "1,001-2,500", ">2,500"
  ),
  Estimate = c(0.3, 0.1, 0.15, 0.5, 0.05, 0.75, 1.2, 0.4, 0.6),
  SE = c(0.1, 0.05, 0.08, 0.2, 0.07, 0.3, 0.4, 0.15, 0.25)
)

# Calculate 95% confidence intervals
simple_data$CI_lower <- simple_data$Estimate - 1.96 * simple_data$SE
simple_data$CI_upper <- simple_data$Estimate + 1.96 * simple_data$SE
multiple_data$CI_lower <- multiple_data$Estimate - 1.96 * multiple_data$SE
multiple_data$CI_upper <- multiple_data$Estimate + 1.96 * multiple_data$SE

# Plot for Simple Regression Model (Panel A)
plot_A <- ggplot(simple_data, aes(x = Estimate, y = Predictor)) +
  geom_point(color = "red") +
  geom_errorbarh(aes(xmin = CI_lower, xmax = CI_upper), color = "red", height = 0.2) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey")

# Plot for Multiple Regression Model (Panel B)
plot_B <- ggplot(multiple_data, aes(x = Estimate, y = Predictor)) +
  geom_point(color = "red") +
  geom_errorbarh(aes(xmin = CI_lower, xmax = CI_upper), color = "red", height = 0.2) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey")

bind_data <- bind_rows(
  simple_data %>% mutate(type = "single"),
  multiple_data %>% mutate(type = "multiple")
)

bind_data
#>                        Predictor Estimate   SE CI_lower CI_upper     type
#> 1  Aware that humans can get TBD     0.30 0.10   0.1040   0.4960   single
#> 2  Aware that humans can get TBD     0.30 0.10   0.1040   0.4960 multiple
#> 3                         Female     0.10 0.05   0.0020   0.1980 multiple
#> 4                          44-59     0.15 0.08  -0.0068   0.3068 multiple
#> 5                          60-80     0.50 0.20   0.1080   0.8920 multiple
#> 6                    High school     0.05 0.07  -0.0872   0.1872 multiple
#> 7            Technical education     0.75 0.30   0.1620   1.3380 multiple
#> 8                     University     1.20 0.40   0.4160   1.9840 multiple
#> 9                    1,001-2,500     0.40 0.15   0.1060   0.6940 multiple
#> 10                        >2,500     0.60 0.25   0.1100   1.0900 multiple

bind_data %>% 
  mutate(type = fct_relevel(type,"single")) %>% 
  mutate(
    Predictor = fct_relevel(
      Predictor,"Aware that humans can get TBD",
      "44-59", "60-80","1,001-2,500", ">2,500"
      ),
    Predictor = fct_rev(Predictor)
  ) %>% 
  ggplot(aes(x = Estimate, y = Predictor)) +
  geom_point(color = "red") +
  geom_errorbarh(aes(xmin = CI_lower, xmax = CI_upper), color = "red", height = 0.2) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +
  facet_grid(type~.,scales = "free",space = "free")

Created on 2024-11-01 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment