簡體   English   中英

R:ggplot堆疊的條形圖,y軸上有計數,但百分比為標簽

[英]R: ggplot stacked bar chart with counts on y axis but percentage as label

我正在尋找一種用百分比標記堆積條形圖的方法,而y軸顯示原始計數(使用ggplot)。 這是不帶標簽的情節的MWE:

library(ggplot2)
df <- as.data.frame(matrix(nrow = 7, ncol= 3,
                       data = c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6", "ID7",
                                "north", "north", "north", "north", "south", "south", "south",
                                "A", "B", "B", "C", "A", "A", "C"),
                      byrow = FALSE))

colnames(df) <- c("ID", "region", "species")

p <- ggplot(df, aes(x = region, fill = species))
p  + geom_bar()

我的桌子要大得多,R很好地統計了每個地區的不同物種。 現在,我想同時顯示原始計數值(最好在y軸上)和百分比(作為標簽),以比較區域之間物種的比例。

我使用geom_text()嘗試了很多東西,但我認為與其他問題( 例如,這個問題geom_text()的主要區別是

  • 我沒有單獨的列用於y值(它們只是每個區域中不同物種的計數),並且
  • 我需要每個區域的標簽總計達100%(因為它們被認為代表了不同的種群),而不是整個圖的所有標簽。

任何幫助深表感謝!!

如@Gregor所述,分別匯總數據,然后將數據摘要輸入ggplot。 在下面的代碼中,我們使用dplyr創建摘要:

library(dplyr)

ggplot(df %>% count(region, species) %>%    # Group by region and species, then count number in each group
         mutate(pct=n/sum(n),               # Calculate percent within each region
                ypos = cumsum(n) - 0.5*n),  # Calculate label positions
       aes(region, n, fill=species)) +
  geom_bar(stat="identity") +
  geom_text(aes(label=paste0(sprintf("%1.1f", pct*100),"%"), y=ypos))

在此處輸入圖片說明

更新:dplyr 0.5及更高版本中,您不再需要提供y值以使文本在每個小節內居中。 相反,您可以使用position_stack(vjust=0.5)

ggplot(df %>% count(region, species) %>%    # Group by region and species, then count number in each group
         mutate(pct=n/sum(n)),              # Calculate percent within each region
       aes(region, n, fill=species)) +
  geom_bar(stat="identity") +
  geom_text(aes(label=paste0(sprintf("%1.1f", pct*100),"%")), 
            position=position_stack(vjust=0.5))

我同意Johanna。 您可以嘗試:

d <- aggregate(.~region+species, df, length)
d$percent <- paste(round(ID/sum(ID)*100),'%',sep='')
ggplot(d, aes(region, ID, fill=species)) + geom_bar(stat='identity') + 
  geom_text(position='stack', aes(label=paste(round(ID/sum(ID)*100),'%',sep='')), vjust=5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM