我是 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 變數,而且我不知道如何修復它,因為與其他方法相比,我使用 fill 的方式有點特殊我已經看到它用 x 和 y 變數完成的方式。我真的不知道我什至會為 x 變數添加什么,因為填充是每種型別回應的百分比(不同的回應型別以級別顯示)。
任何幫助,將不勝感激!如果這很重要,很樂意回答有關資料集的任何問題。
這是兩個相關列的外觀示例(未使用 head,因為此資料集中的變數太多)。基本上,它們會顯示受訪者屬于哪一方,如果他們非常同意,有些同意,等等。

這是兩個變數的 dput 輸出:
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"))
uj5u.com熱心網友回復:
要將百分比放在條形中間,請使用position_fill(vjust = 0.5)并計算geom_text. 這些比例是總值的比例,而不是柱形。
library(ggplot2)
colors <- c("#00405b", "#008dca", "#c0beb8", "#d70000", "#7d0000")
colors <- setNames(colors, levels(newDoto$Q29_1String))
ggplot(newDoto, aes(pid3lean, fill = Q29_1String))
geom_bar(position = position_fill())
geom_text(aes(label = paste0(..count../sum(..count..)*100, "%")),
stat = "count",
colour = "white",
position = position_fill(vjust = 0.5))
scale_fill_manual(values = colors)
coord_flip()

包scales具有自動格式化百分比的功能。
ggplot(newDoto, aes(pid3lean, fill = Q29_1String))
geom_bar(position = position_fill())
geom_text(aes(label = scales::percent(..count../sum(..count..))),
stat = "count",
colour = "white",
position = position_fill(vjust = 0.5))
scale_fill_manual(values = colors)
coord_flip()

編輯
在按條要求比例的
要添加到情節的主題
這theme是
uj5u.com熱心網友回復:
您首先需要使用dplyr包計算百分比:
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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/400759.html
下一篇:提取嵌套字典
