I didn't understand your comment about CMM_binary, and your example didnt include anything called MM_binary for me to understand your intentions aside from your follow up post text that said you wanted to plot the proportion of disease cases by age. I think this delivers that:
library(tidyverse)
conditions_df <- tribble(
~ID, ~Age, ~Sex, ~CarsQuintile, ~age_group, ~CarsQuintile_group, ~Diabetes, ~Asthma, ~Stroke, ~Heart.attack, ~COPD, ~Hypertension, ~Eczema, ~Depression,
1L, 18L, 1L, 2L, "18 - 24", 3L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L,
2L, 77L, 1L, 1L, "65 - 74", 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L,
3L, 25L, 1L, 3L, "25 - 34", 4L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
4L, 30L, 1L, 1L, "25 - 34", 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
5L, 54L, 1L, 1L, "55 - 64", 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L,
6L, 78L, 1L, 5L, "75 - 84", 5L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L,
7L, 69L, 1L, 1L, "65 - 74", 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L,
8L, 62L, 1L, 1L, "55 - 64", 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
9L, 68L, 1L, 5L, "55 - 64", 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L,
10L, 63L, 1L, 1L, "55 - 64", 3L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L
)
# add more here as needed
dis_vec <- c("Diabetes", "Asthma", "Stroke")
(smry_cond <- conditions_df %>%
group_by(age_group) %>%
summarise(across(
.cols = dis_vec,
.fns = ~ sum(. == 1) / n()
)))
(smry_long <- pivot_longer(smry_cond,
cols = -age_group
) %>%
mutate(age_group =
factor(age_group)))
ggplot(data = smry_long) +
aes(x = age_group, y = value,
group = name, color = name) +
geom_line(size = 2, alpha = .7)