簡體   English   中英

將標簽添加到百分比堆積條形圖 ggplot2

[英]Adding labels to percentage stacked barplot ggplot2

我是 ggplot 的新手,希望為我正在為其進行可視化的數據集獲得一些幫助。

這是我當前的代碼:

#create plot
plot <- ggplot(newDoto, aes(y = pid3lean, weight = weight, fill = factor(Q29_1String, levels = c("Strongly disagree","Somewhat disagree", "Neither agree nor disagree", "Somewhat agree", "Strongly agree")))) + geom_bar(position = "fill", width = .732) 
#fix colors
plot <- plot + scale_fill_manual(values = c("Strongly disagree" = "#7D0000", "Somewhat disagree" = "#D70000","Neither agree nor disagree" = "#C0BEB8", "Somewhat agree" = "#008DCA", "Strongly agree" = "#00405B")) 
#fix grid
plot <- plot + guides(fill=guide_legend(title="29")) + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(panel.border = element_blank()) + theme(axis.ticks = element_blank()) + theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank()) + theme(axis.text.x=element_blank()) + theme(text=element_text(size=19,  family="serif")) + theme(axis.text.y = element_text(color="black")) + theme(legend.position = "top") + theme(legend.text=element_text(size=12)) 
#plot graph
plot

這將創建此條形圖: 在此處輸入圖像描述

現在我遇到的問題是嘗試在這些條上添加百分比標簽。 我想添加顯示每個段百分比的文本,居中並以白色字母顯示。

不幸的是,我在添加 geom_text 時遇到了一些麻煩,因為它經常給我錯誤,因為我沒有 x 變量而且我不知道如何修復它,因為我使用填充的方式與其他方式相比有點特殊我已經看到它使用 x 和 y 變量完成的方式。 考慮到填充是每種響應類型的百分比(不同的響應類型顯示在級別中),我真的不知道我什至會為 x 變量添加什么。

任何幫助,將不勝感激。 如果這很重要,很高興回答有關數據集的任何問題。

這是兩個相關列的外觀示例(沒有使用 head 因為這個數據集中有很多變量)。 基本上,它們顯示了受訪者屬於哪一方,以及他們是否強烈同意、有點同意等。

數據樣本

這是兩個變量的 dput 的 output:

structure(list(pid3lean = structure(c("Democrats", "Democrats", 
"Democrats", "Democrats", "Independents", "Democrats", "Republicans", 
"Independents", "Republicans", "Democrats", "Democrats", "Independents", 
"Democrats", "Republicans", "Democrats", "Democrats", "Democrats", 
"Democrats", "Democrats", "Republicans"), label = "pid3lean", format.spss = "A13", display_width = 15L), 
    Q29_1String = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 5L, 4L, 
    1L, 1L, 2L, 5L, 1L, 5L, 1L, 1L, 1L, 5L, 1L, 3L), .Label = c("Strongly agree", 
    "Somewhat agree", "Neither agree nor disagree", "Somewhat disagree", 
    "Strongly disagree"), class = "factor")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

要將百分比放在條形中間,請使用position_fill(vjust = 0.5)並計算geom_text中的比例。

library(ggplot2)

ggplot(newDoto, aes(pid3lean, fill = Q29_1String)) +
  geom_bar(position = position_fill()) +
  geom_text(aes(label = paste(..count../sum(..count..)*100, "%")),
            stat = "count",
            position = position_fill(vjust = 0.5)) +
  coord_flip()

在此處輸入圖像描述

您首先需要使用dplyr package 計算百分比:

library(dplyr)
newDoto <- newDoto %>% group_by(pid3lean) %>%
  count(Q29_1String) %>%
  mutate(perc = n/sum(n)) %>%
  select(-n)

使用現有代碼,您只需在代碼末尾添加以下行:

plot + 
  geom_text(stat = 'count', aes(label = perc), position = position_fill(vjust = 0.5), size = 3, color = "white")

這是另一種方法。

library(tidyverse)

df %>% 
  group_by(pid3lean) %>% 
  count(Q29_1String) %>% 
  ungroup() %>% 
  mutate(pct = n/sum(n)) %>% 
  mutate(Q29_1String = as.factor(Q29_1String)) %>% 
  ggplot(aes(x = pid3lean, y = pct, fill = Q29_1String)) +
  geom_col(position = "fill", width = .732) +
  scale_fill_manual(values = c("Strongly disagree" = "#7D0000", "Somewhat disagree" = "#D70000","Neither agree nor disagree" = "#C0BEB8", "Somewhat agree" = "#008DCA", "Strongly agree" = "#00405B")) +
  coord_flip()+
  geom_text(aes(label = scales::percent(pct)), 
            position = position_fill(vjust = 0.5),size=5, color="white",
            ) + guides(fill=guide_legend(title="29")) + 
  theme_bw() + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.border = element_blank(), 
        axis.ticks = element_blank(), 
        axis.title.y=element_blank(), 
        axis.title.x=element_blank(), 
        axis.text.x=element_blank(), 
        text=element_text(size=19,  family="serif"), 
        axis.text.y = element_text(color="black"),
        legend.position = "top",
        legend.text=element_text(size=12)
        ) 

在此處輸入圖像描述

暫無
暫無

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

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