我想要一個條形圖,顯示我正在研究的人口分布,這個條形圖以年齡為 x 軸(計數為 y),填充是種族。我想用 geom_scatter 覆寫其相應組內的一些主題。但是它會創建自己的軸。我無法分享資料,但這是一個虛擬的小標題
df = tribble(
~id, ~agegroup, ~ethnicity,
#--|--|----
"a", "20s", "African Descent",
"b", "30s", "White",
"c", "50s", "White",
"d", "40s", "Hispanic",
"e", "20s", "White",
"f", "30s", "Hispanic",
"g", "20s", "Hispanic",
"h", "30s", "White",
"i", "20s", "African Descent",
"j", "30s", "White",
"k", "50s", "White",
"l", "20s", "White",
"m", "30s", "Hispanic",
"n", "20s", "Hispanic",
"o", "30s", "White",
)
df
dmplot <- ggplot(df, aes(x = agegroup, fill = ethnicity ))
geom_bar(stat = "count")
labs(
x = "Age Group",
y = paste0("Population (total = ", df %>% nrow(), ")"))
geom_jitter(df,
aes(x = agegroup,
y = ethnicity)) # this is where I would need to retrieve the geom_bar fill location
dmplot
uj5u.com熱心網友回復:
這有點hacky,但我認為這樣的事情可以做到。不幸的是,您似乎無法為美學指定抖動高度,但您也許可以找到另一種方法使高度依賴于矩形的高度。
df = tribble(
~id, ~agegroup, ~ethnicity,
#--|--|----
"a", "20s", "African Descent",
"b", "30s", "White",
"c", "50s", "White",
"d", "40s", "Hispanic",
"e", "20s", "White",
"f", "30s", "Hispanic",
"g", "20s", "Hispanic",
"h", "30s", "White",
"i", "20s", "African Descent",
"j", "30s", "White",
"k", "50s", "White",
"l", "20s", "White",
"m", "30s", "Hispanic",
"n", "20s", "Hispanic",
"o", "30s", "White",
)
df_2 <- df %>%
count(agegroup, ethnicity) %>%
group_by(agegroup ) %>%
mutate(top_rect = cumsum(n),
bottom_rect = lag(top_rect, default = 0))
df_2_uncounted <- df_2 %>%
ungroup() %>%
uncount(n)
ggplot(df_2)
geom_rect( aes(xmin = as.numeric(as.factor(agegroup)) - .45,
xmax= as.numeric(as.factor(agegroup)) .45,
ymin = bottom_rect,
ymax = top_rect,
fill = ethnicity ))
geom_jitter(data = df_2_uncounted,
aes(x = as.numeric(as.factor(agegroup)),
y = (bottom_rect top_rect)/2),
width = .3,
height = .5)
scale_x_continuous(breaks = unique(as.numeric(as.factor(df_2$agegroup))),
labels = levels(as.factor(df_2$agegroup)))
labs(
x = "Age Group",
y = paste0("Population (total = ", df_2_uncounted %>% nrow(), ")"))

更新
現在有標簽
df_2_uncounted <- df_2 %>%
ungroup() %>%
uncount(n)%>%
arrange(agegroup, ethnicity) %>%
group_by(agegroup, ethnicity) %>%
mutate(id2 = 1:n()) %>%
left_join(df %>%
arrange(agegroup, ethnicity) %>%
group_by(agegroup, ethnicity) %>%
mutate(id2 = 1:n()),
by = c("agegroup", "ethnicity", "id2"))
ggplot(df_2)
geom_rect( aes(xmin = as.numeric(as.factor(agegroup)) - .45,
xmax= as.numeric(as.factor(agegroup)) .45,
ymin = bottom_rect,
ymax = top_rect,
fill = ethnicity ))
geom_jitter(data = df_2_uncounted,
aes(x = as.numeric(as.factor(agegroup)),
y = (bottom_rect top_rect)/2),
position = position_jitter(seed = 1, height =0.5))
geom_text(data = df_2_uncounted,
aes(x = as.numeric(as.factor(agegroup)),
y = (bottom_rect top_rect)/2,
label = id),
position = position_jitter(seed = 1, height =0.5))
scale_x_continuous(breaks = unique(as.numeric(as.factor(df_2$agegroup))),
labels = levels(as.factor(df_2$agegroup)))
labs(
x = "Age Group",
y = paste0("Population (total = ", df_2_uncounted %>% nrow(), ")"))

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/338258.html
下一篇:將圖例插入不同的圖形
