for this dataset
structure(list(triplet_id = c("100_2015-11-26_2016-11-09_2017-12-06",
"100_2015-11-26_2016-11-09_2017-12-06", "100_2015-11-26_2017-02-15_2017-12-06",
"100_2015-11-26_2017-02-15_2017-12-06", "100_2015-11-26_2017-02-15_2018-05-09",
"100_2015-11-26_2017-02-15_2018-05-09", "100_2016-05-11_2017-02-15_2017-12-06",
"100_2016-05-11_2017-02-15_2017-12-06", "100_2016-05-11_2017-02-15_2018-05-09",
"100_2016-05-11_2017-02-15_2018-05-09"), age_group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("<5", "5-9", "10-18", ">18"), class = "factor"),
visit_type = c("visit_number", "visit_number", "visit_number",
"visit_number", "visit_number", "visit_number", "visit_number",
"visit_number", "visit_number", "visit_number"), visit_type_1 = c("visit2",
"visit3", "visit2", "visit3", "visit2", "visit3", "visit2",
"visit3", "visit2", "visit3"), Item = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19"), class = "factor"), Item_score = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("-1", "0",
"1"), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
I want to add the total unique ID (triplet_id) to each panel title for each age_group. I wrote this code, but it doesn't work, neither for the position of the text nor for the actual count
pl <- ggplot(data = graph,aes(y= Item, fill = Item_score))+
geom_bar(stat="count", position = position_fill(reverse = TRUE),color = "black") +
scale_y_discrete(limits=rev)+
scale_fill_manual(values = c("red", "white", "seagreen3"))+
theme_classic() +
labs(y ="items\n", x = "Percentage")+
scale_x_continuous(labels = scales::percent,breaks = seq(0, 1, .25))+
facet_grid(visit_type_1~age_group, labeller = labeller(visit_type_1 = time.labs))+
theme(legend.position = "none",text = element_text(size = 30),strip.text.x = element_text(size = 30),
strip.text.y = element_text(size = 30),panel.spacing.x=unit(1.5, "lines"),panel.spacing.y=unit(2, "lines"))+
geom_text(aes(label = paste0("Count: ", length(unique(triplet_id)))),
x = 5, y = 5)+
labs(title = "A")
pl
do you have any suggestion? Thank you
You can create a custom labeller function that counts unique triplet IDs and returns the count for each age_group
:
label_func <- function(age_grp) {
n_triplet_ids <- df |>
count(age_group, triplet_id) |>
count(age_group) |>
pull(var = n)
return(paste0("Age Group: ", age_grp, " (N triplets: ", n_triplet_ids, ")"))
}
df |>
ggplot(aes(y = Item, fill = Item_score)) +
geom_bar(stat="count") +
facet_grid(visit_type_1 ~ age_group,
labeller = labeller(age_group = label_func))
Updated data:
# A tibble: 10 × 6
triplet_id age_group visit_type visit_type_1 Item Item_score
<chr> <fct> <chr> <chr> <fct> <fct>
1 100_2015-11-26_2016-11-09_2017-12-06 5-9 visit_number visit2 1 0
2 100_2015-11-26_2016-11-09_2017-12-06 5-9 visit_number visit3 1 0
3 100_2015-11-26_2017-02-15_2017-12-06 5-9 visit_number visit2 1 0
4 100_2015-11-26_2017-02-15_2017-12-06 5-9 visit_number visit3 1 0
5 100_2015-11-26_2017-02-15_2018-05-09 5-9 visit_number visit2 1 -1
6 100_2015-11-26_2017-02-15_2018-05-09 5-9 visit_number visit3 1 -1
7 100_2016-05-11_2017-02-15_2017-12-06 5-9 visit_number visit2 1 0
8 100_2016-05-11_2017-02-15_2017-12-06 10-18 visit_number visit3 1 0
9 100_2016-05-11_2017-02-15_2018-05-09 10-18 visit_number visit2 1 0
10 100_2016-05-11_2017-02-15_2018-05-09 10-18 visit_number visit3 1 -1