I am currently creating a bar graph with a series of related variables. When answering my survey questions, not every variable was applicable to each person. People had the option of choosing, increased, same, decreased, not applicable. Meaning, each variable included in my bar graph has varying NA responses.
I want to create a bar graph that uses the complete cases for each variable (not the complete cases for the dataset) that only displays the "Decreased" value. I do not want to include the NA in the overall value for each variable, which means that each variable has a different total N. How do I go about doing this? When I run functions like df <- na.omit(df) it removes all the NAs from the dataset, which is not what I want.
This is the code that I have written so far to set up my dataset.
df <- select(dataset, variable1, variable2, variable3, variable4,
variable5, variable6, variable7)
df2 <-df %>%
pivot_longer(everything(), names_to="vars", values_to ="value") %>%
group_by(vars, value) %>%
dplyr::summarise(n = n()) %>%
mutate(pct = (n / sum(n)*100)) %>%
ungroup() %>%
subset(value == "Decreased")
plot <-df2 %>%
ggplot() +
geom_col( aes(vars, pct),
fill="black")