问题描述
我正在尝试在100%堆积的条形图上标注标签百分比(参见图片)。我在下面的第一个块中计算百分比,然后运行ggplot。 您知道我在做什么错吗,为什么我的百分比以这种方式显示?
library(scales)
percentData = surveyds %>%
group_by(qtext,X_Gender,response) %>%
summarise(N = n()) %>%
mutate(prop = N/sum(N))
percentData$prop = label_percent(accuracy = 1)(percentData$prop) #make percent from decimal
surveyds$responsef = factor(
surveyds$response,levels = c("Poor","Fair","Good","Very good","Excellent"),ordered = TRUE
) #Order bars
#Run visual
ggplot(data =subset(surveyds,!is.na(responsef)))+ #responsef is my response factored
geom_bar(aes(x = X_Gender,fill = responsef),position = 'fill',width = 0.5)+
facet_grid(rows = vars(qtext))+ #qtext is my question text
coord_flip()+
ylab('Proportion')+
xlab('question')+
ggtitle('Change')+
geom_text(data = percentData,aes(y = N,label = prop,x = X_Gender),position=position_fill(vjust=0.5))
解决方法
我知道了。需要在我的geom_text中添加填充参数。
payment_method_category