Вопрос
Как добавить метки, показывающие количество наблюдений вдоль график плотности?
Данные
Мой набор данных:
mwe <- structure(list(Gender = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("Female", "Male"), class = "factor"),
Age = c(23, 23, 23, 23, 23, 23, 39, 39, 39, 39, 39, 39, 30,
30, 30, 30, 30, 30, 30, 30, 24, 24, 24, 24, 24, 24, 24, 24,
18, 18, 18, 18, 18, 18, 23, 23, 23, 23, 23, 23, 23, 23, 26,
26, 26, 26, 26, 26, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 30, 30, 30, 30, 30, 30, 20, 20, 20, 20, 20,
20, 25, 25, 25, 25, 25, 25, 25, 25, 23, 23, 23, 23, 23, 23,
23, 23, 38, 38, 38, 38, 38, 38, 22, 22, 22, 22, 22, 22, 29,
29, 29, 29, 29, 29, 21, 21, 21, 21, 21, 21, 23, 23, 23, 23,
23, 23, 25, 25, 25, 25, 25, 25, 24, 24, 24, 24, 24, 24, 21,
21, 21, 21, 21, 21, 27, 27, 27, 27, 27, 27, 24, 24, 24, 24,
24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 23,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 21, 21,
21, 21, 27, 27, 27, 27, 27, 27, 34, 34, 34, 34, 34, 34, 26,
26, 26, 26, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 39, 39,
39, 39, 39, 39, 26, 26, 26, 26, 26, 26), KmEuc = structure(c(1L,
1L, 1L, 1L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L,
1L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 3L, 3L, 2L, 3L,
2L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 2L,
3L, 2L, 3L, 3L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 2L,
2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L,
2L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 2L,
2L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 2L,
2L, 3L, 3L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L,
3L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor")), class = "data.frame", row.names = c(NA,
-218L))
Я хочу показать распределение по возрасту, используя график плотности:
Код
p1 <- ggplot() +
geom_freqpoly(aes(x = Age, color = KmEuc), stat = 'density', position = 'dodge', data=mwe) +
scale_color_manual(guide = guide_legend(),name = 'Clusters',values = c("#E31A1C","#332288", "#66A61E"), labels = c("Pie", "Carrot", "Rice")) +
theme_light(base_size=14) +
facet_grid(facets = Gender ~ .) +
theme(axis.title.x = element_blank(),axis.title.y = element_blank())
Trial
Чтобы добавить метки счетчиков, я попробовал следующее:
dfLabels <- mwe %>%
select(c(Age, Gender, KmEuc)) %>%
group_by(Age, Gender, KmEuc) %>%
dplyr::summarise(N = n())
p1 + geom_label(data = dfLabels, aes(x = Age, y = 0.01, label = N), size = 3, vjust = 0, hjust = 0)
С y=0.01
Я мог бы показать N
только на фиксированной линии по оси Y, как заставить N
появляться вдоль функции плотности в этом случае?