我正在尋找以下問題的解決方案:我的資料包含兩個因子變數EDU和LEVEL. 可重現的資料樣本在這里:
structure(list(EDU = structure(c(3L, 1L, 2L, 2L, 3L, 2L, 3L,
2L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 2L, 3L, 3L, 1L, 2L, 3L, 2L,
2L, 2L, 1L, 1L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 2L, 3L,
3L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 1L), .Label = c("A",
"B", "C"), class = "factor"), LEVEL = structure(c(3L, 3L, 4L,
2L, 4L, 3L, 1L, 2L, 2L, 1L, 3L, 2L, 3L, 2L, 3L, 3L, 4L, 2L, 2L,
4L, 1L, 2L, 3L, 3L, 1L, 4L, 2L, 3L, 1L, 1L, 2L, 3L, 1L, 2L, 1L,
4L, 3L, 1L, 4L, 3L, 4L, 1L, 4L, 2L, 4L, 1L, 1L, 4L, 3L, 1L), .Label = c("1",
"2", "3", "4"), class = "factor")), class = "data.frame", row.names = c(NA,
-50L))
使用這些資料,我想繪制一個條形圖,在 x 軸上ggplot2顯示分組變數EDU,在 y 軸上顯示 的累積百分比LEVEL。此外,我想添加第四個條形圖,其中包含百分比LEVEL但不分組的百分比EDU- 有點像“整體條形”。此外,我想在圖中添加百分比標簽,以便每個LEVEL標簽都標有相應的相對頻率,如 this 
到目前為止,這個情節看起來不錯。但如上所述,我的目標是添加百分比標簽,geom_text除了現有的三個標簽之外,可能還有第四個“整體條”。對于百分比標簽,我還嘗試制作 aprop.table并添加了帶有相應道具和的百分比標簽annotate:
props <- prop.table(table(df$EDU, df$LEVEL), margin=1)
ggplot(df, aes(x=EDU, fill=LEVEL))
geom_bar(position="fill")
scale_y_continuous(labels = scales::percent)
annotate("text", x="A", y=.15, label=scales::percent(props[1,4]))
annotate("text", x="B", y=.10, label=scales::percent(props[2,4]))
annotate("text", x="C", y=.275, label=scales::percent(props[3,4]))
annotate("text", x="A", y=.375, label=scales::percent(props[1,3]))
annotate("text", x="B", y=.275, label=scales::percent(props[2,3]))
annotate("text", x="C", y=.625, label=scales::percent(props[3,3]))
annotate("text", x="A", y=.66, label=scales::percent(props[1,2]))
annotate("text", x="B", y=.5, label=scales::percent(props[2,2]))
annotate("text", x="C", y=.78, label=scales::percent(props[3,2]))
annotate("text", x="A", y=.9, label=scales::percent(props[1,1]))
annotate("text", x="B", y=.9, label=scales::percent(props[2,1]))
annotate("text", x="C", y=.9, label=scales::percent(props[3,1]))
這導致以下情節:

這對我來說似乎很麻煩,尤其是當我想創建多個繪圖并且必須分別注釋每個百分比時。在這里,問題可能是我如何annotate以“自動”方式設定 y 引數,讓 R 為我定位標簽。
不幸的是,關于“整體酒吧”問題,我不知道如何解決。
我很感激任何幫助!
uj5u.com熱心網友回復:
請放心:您的經驗越豐富,您就越不會害怕事先準備資料。您會發現,首先將資料準備為您想要繪制的內容,然后再進行繪制通常更容易和更清晰。不要試圖在 ggplot2 中做任何事情,這會很痛苦。
代碼中的注釋
library(tidyverse)
## create a percentage column manually
df_perc <-
df %>%
count(EDU, LEVEL) %>%
group_by(EDU) %>%
mutate(perc = n*100/sum(n))
## for the total, create a new data frame and bind to the old one
total <-
df_perc %>%
group_by(LEVEL) %>%
summarise(n = sum(n)) %>%
## ungroup for the total
ungroup() %>%
## add EDU column called total, so you can bind it and plot it easily
mutate(perc= n*100/sum(n), EDU = "Total")
## now bind them and plot them
bind_rows(df_perc, total) %>%
ggplot(aes(x=EDU, y = perc, fill=LEVEL))
## use geom_col, and remove position = fill
geom_col()
# now you can add the labels easily as per all those threads
geom_text(aes(label = paste(round(perc, 2), "%")), position = position_stack(vjust = .5))
## you can either change the y values, or use a different scale factor
scale_y_continuous("Percent", labels = function(x) scales::percent(x, scale = 1))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394825.html
上一篇:直接使用HDF檔案(不是tif檔案)在回圈中堆疊光柵
下一篇:根據每周日期列,每年拆分行
