질문
밀도 플롯을 따라 관찰 수를 표시하는 레이블을 추가하는 방법은 무엇입니까?
데이터
내 데이터 세트 :
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())
시도
카운트 레이블을 추가하려면 다음을 시도했습니다.
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
이 경우에는 밀도 함수를 따라 나타 납니까?
이 시도. 카운트 계산 외에도 각 연령에 대한 밀도도 계산합니다. 여기 에서 일반적인 아이디어를 빌 렸지만 문제에 적용하고 tidyverse
접근 방식을 사용했습니다 .
library(ggplot2)
library(purrr)
library(dplyr)
library(tidyr)
dfLabels <- mwe %>%
select(Age, Gender, KmEuc) %>%
group_by(Gender, KmEuc) %>%
nest() %>%
# Compute density
mutate(dens = purrr::map(data, ~ density(.$Age))) %>%
# Unique Ages
mutate(age_uniq = purrr::map(data, ~ unique(.$Age))) %>%
unnest(age_uniq)
dfLabels1 <- dfLabels %>%
# Compute "y" by interpolation and count
mutate(label.y = purrr::map2_dbl(age_uniq, dens, ~approx(.y$x, .y$y, .x)$y),
label.n = purrr::map2_dbl(age_uniq, data, ~ sum(.y$Age == .x))) %>%
select(Gender, KmEuc, Age = age_uniq, label.y, label.n)
p1 <- ggplot() +
geom_freqpoly(aes(x = Age, color = KmEuc), stat = 'density', position = 'dodge', data=mwe) +
geom_text(aes(x = Age, y = label.y, color = KmEuc, label = label.n),
position = 'dodge', vjust = 0, show.legend = FALSE, data=dfLabels1) +
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())
p1
#> Warning: Width not defined. Set with `position_dodge(width = ?)`
#> Warning: Width not defined. Set with `position_dodge(width = ?)`
reprex 패키지 (v0.3.0)에 의해 2020-04-11에 생성됨
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다