Search code examples
rggplot2

Visualize weighted percentage of panel data in ggplot?


I have panel data organized like below, where I have repeated measures of v1-v4 which are binary, along with columns with survey weights and gender.

My goal is to visualize the weighted percent of respondents who indicate =1 for v1 in any wave/year, then again for v2, then for v3, then for v4. So v1/v2/v3/v4 would be ticks along one axis and the other axis is percent. How can I calculate and subsequently visualize the percentages while incorporating the survey weights?

Example data:

d <- data.frame(
  respondent_id = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 10),
  custom_wt = c(24, 24, 24, 26, 26, 26, 12, 12, 12, 14, 14, 14, 33, 33, 33, 32, 32, 32, 9, 9, 9, 10, 10, 10, 10),
  year = c(2002, 2004, 2006, 2002, 2004, 2006, 2002, 2004, 2006, 2002, 2004, 2006, 2002, 2004, 2006, 2002, 2004, 2006, 2002, 2004, 2006, 2002, 2004, 2006, 2006),
  v1 = c(0, 0, 1, NA, 1, 1, 0, NA, NA, 1, NA, NA, 1, 0, 0, 0, 0, 0, 1, 1, NA, 1, 0, 0, 0),
  v2 = c(0, 0, 1, NA, 1, 1, 0, NA, 1, NA, 1, 1, 0, NA, 1, 0, 1, 1, NA, 1, 1, 0, NA, 1, 1),
  v3 = c(0, 0, NA, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, NA, 0, 0, 1, 0, 0, 0, 0, 0, 0),
  v4 = c(NA, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, NA, 0, 0, 0, 1, 0, 0, NA, NA),
  gender = c(0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0)
)

Solution

  • For example:

    library(tidyverse)
    d |> 
      pivot_longer(v1:v4) |> 
      summarise(
        mean_value = weighted.mean(value, custom_wt, na.rm = TRUE), 
        .by = c(name, gender)
      ) |> 
      ggplot(aes(name, mean_value, fill = factor(gender))) +
      geom_col(position = 'dodge') + 
      scale_y_continuous(expand = expansion(c(0, 0.05)), labels = scales::label_percent()) +
      theme_classic() + theme(legend.position = "top")
    

    enter image description here