title: “Plots for Yechiam” author: “Ami” format: html: code-fold: true code-summary: “Show Code” embed-resources: true linestretch: 1.1 page-layout: full —

I first used stacked bars with ggplot and patchwork.

pacman::p_load(dplyr, ggplot2, patchwork, scales, tidyverse)

t1 <- tibble(
  g1 = c(rep("Uncomplicated Type 2 Diabetes", 3), rep("Diabetic Retinopathy", 3)),
  g2 = rep(c("Female", "Male", "Skip"), 2),
  Percent = c(58.36, 40.3, 1.29,
    51.15, 47.38, 1.48)
)

t2<- tibble(
  g1 = c(rep("Uncomplicated Type 2 Diabetes", 4), rep("Diabetic Retinopathy", 4)),
  g2 = rep(c("18 to 50", "51 to 60", "61 to 70", "71 and older"), 2),
  Percent = c(21.02, 26.23, 29.59, 23.17,
    12.68, 25.28, 33.77, 28.27)
)

t3<- tibble(
  g1 = c(rep("Uncomplicated Type 2 Diabetes", 6), rep("Diabetic Retinopathy", 6)),
  g2 = factor(rep(c("Asian", "Black", "Hispanic", "Other", "White", "Skip"), 2),
              levels =c("White","Black","Hispanic","Asian","Other","Skip") ),
  Percent = c(2.11, 26.01, 20.94, 2.08, 45.3, 3.56,
    2.37, 27.02, 29.81, 2.76, 34.44, 3.61)
  
)




#g2 NEEDS TO BE CHANGED TO MATCH LEVELS FROM t below
t4<- tibble(
  g1 = c(rep("Uncomplicated Type 2 Diabetes", 6), rep("Diabetic Retinopathy", 6)),
  g2 = rep(c("0-10k", "10-25k", "25-50k", "50-100k", "> 100k", "Skip"), 2),
  Percent = c(16.26, 16.19, 15.5, 15.45, 11.24, 25.36,
    14.43, 18.69, 15.08, 12.28, 7.43, 32.09)
  
)

t5<- tibble(
  g1 = c(rep("Uncomplicated Type 2 Diabetes", 6), rep("Diabetic Retinopathy", 6)),
  g2 = rep(c("LT 11", "HS or GED", "Some College", "BS", "MS+", "Skip"), 2),
  Percent = c(14.34, 23.13, 28.0, 16.41, 13.18, 4.7,
    19.09, 23.06, 26.85, 16.39,10.49, 4.12)
  
)

t6<- tibble(
  g1 = c(rep("Uncomplicated Type 2 Diabetes", 3), rep("Diabetic Retinopathy", 3)),
  g2 = rep(c("YES", "NO", "Skip"), 2),
  Percent = c(91.2, 5.21, 3.67, 90.71, 5.79, 3.5)
  
)

stacked_bar <- function(t, l_title){
  ggplot(t,
    aes(x = Percent, y = g1, fill = fct_rev(g2))) +
    geom_col(alpha = 0.75) +
    scale_fill_viridis_d() +
    guides(fill = guide_legend(nrow = 1, reverse = TRUE)) +
    scale_x_continuous(breaks = seq(0, 100, 10), expand = c(0.01, 0.01)) +
    scale_y_discrete(labels = wrap_format(16)) +
    labs(fill = l_title,
      y = element_blank()) +
    theme_light() +
    theme(legend.position = "top",
      text = element_text(size = 8))
}

p1 <- stacked_bar(t1, "Sex at Birth:")
p2 <- stacked_bar(t2, "Age at Survey:")
p3 <- stacked_bar(t3, "Race:")
p4 <- stacked_bar(t4, "Income:")
p5 <- stacked_bar(t5, "Level of Education:")
p6 <- stacked_bar(t6, "Health care:")

p1 / p2 / p3/ p4/p5/p6

t <- bind_rows(
  t1 %>% mutate(characteristic = "Sex at Birth (p<.001)"),
  t2 %>% mutate(characteristic = "Age at Survey (p<.001)"),
  t3 %>% mutate(characteristic = "Race (p<.001)"),
  t4 %>% mutate(characteristic = "Income (p<.001)"),
  t5 %>% mutate(characteristic = "Level of education (p<.001)"),
  t6 %>% mutate(characteristic = "Health care (p=0.389)")
  ) %>%
  mutate(
    g1 = factor(g1, 
      levels = c("Uncomplicated Type 2 Diabetes", "Diabetic Retinopathy")),
    characteristic = factor(characteristic,
      levels = c("Sex at Birth (p<.001)", "Age at Survey (p<.001)", "Race (p<.001)","Income (p<.001)", "Level of education (p<.001)","Health care (p=0.389)" )),
    g2 =factor(g2, #THIS LIST NEEDS TO BE CHANGED TO MATCH LEVELS FROM t1 - t6 above
      levels = c("0-10k", "10-25k", "25-50k", "50-100k", "> 100k","White","Black","Hispanic","Asian","Other","18 to 50", "51 to 60", "61 to 70", "71 and older","Female", "Male", "LT 11", "HS or GED", "Some College", "BS", "MS+","YES", "NO","Skip"))         
    
  )

geom.text.size = 2

ggplot(t,
  aes(x = Percent, y = fct_rev(g2), fill = g2)) +
  geom_col(alpha = 0.75) +
  geom_text(aes(label = sprintf("%.1f", Percent)), nudge_x = 4,size=geom.text.size) +
  scale_fill_viridis_d() +
  expand_limits(x = c(0, 100)) +
  scale_x_continuous(breaks = seq(0, 100, 10), expand = c(0.01, 0.01)) +
  labs(y = element_blank()) +
  facet_grid(characteristic~g1, scales = "free_y", switch = "y", labeller=label_wrap_gen(width = 2,multi_line = TRUE)) +
  theme_light() +
  theme(legend.position = "none",
    strip.placement = "outside",
    strip.text = element_text(color = "#000000"),
    strip.background = element_rect(fill = "#eeeeee"),
    text = element_text(size = 8))