Ggplot填充的带有百分比标签的条形图 [英] Ggplot filled barplot with percentage labels

查看:83
本文介绍了Ggplot填充的带有百分比标签的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个简单的barplot,我将一个变量x(A,B,C,D)分类,另一个用于填充的y(是,否),以及一组观测值,我想要显示一个填充的条形图,每列中都带有百分比标签.

I want to make a simple barplot, I have one variable x (A,B,C,D) categorical, another one y (YES, NO) that I am using to fill, and a set of observations, and I want to display a filled barplot, with percentage labels in each column.

像这样简单的事情:

正确填充的Baplot

到目前为止,ggplot图层系统一直是使用中的噩梦.在已经提出的问题中我找不到任何解决方案.

So far ggplot layer system has been a nightmare to use. And no solutions I was able to find in already asked questions.

x11()
ggplot(data=KS, aes(x=KS$main_category, fill=KS$state)) +
    geom_bar(position="fill") +
    scale_y_continuous(labels = percent) +
    geom_text(aes(label = ..count.., group = KS$state), 
              stat = "count")

到目前为止,这是我得到的,用于定位的部分显示每个类别和状态的计数,为什么它不能显示比例?并且我要避免操纵数据,并在数据框中添加内容.

This is what I got so far and a part for positioning it displays the count for every category and state, why can't it display proportions?. And I want to avoid to manipulate the data and adding stuff to the dataframe.

非常感谢.

请求的数据框

library("ggplot2")
library("scales")

main_category=c('A','A','B','C','D','A','A','B','C','D','A','A','B','C','D','A','A','B','C','D')
state=c('Yes', 'No', 'Yes', 'Yes','Yes', 'No', 'Yes', 'Yes','Yes', 'No', 'Yes', 'Yes','Yes', 'No', 'Yes', 'Yes', 'No', 'No', 'No', 'No')
KS = data.frame(main_category, state)

我能够通过使用隐式ggplot变量来找到自己的解决方案而无需操纵数据集:

I was able to find my own solution without manipulating the dataset by using implicit ggplot variables:

geom_text(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..], label=percent(..count../tapply(..count.., ..x.. ,sum)[..x..]) ),
              stat="count", position=position_fill(0.5), vjust=0.5)

推荐答案

鉴于您的数据,请先计算百分比,然后计算相应的y值,并按照注释中链接的文章中的描述进行绘制:

Given your data calculate the precentage first then calculate the respective y-value and plot it as described in the post you linked in the comment:

library("ggplot2")
library("scales")
library(dplyr)

main_category=c('A','A','B','C','D','A','A','B','C','D','A','A','B','C','D','A','A','B','C','D')
state=c('Yes', 'No', 'Yes', 'Yes','Yes', 'No', 'Yes', 'Yes','Yes', 'No', 'Yes', 'Yes','Yes', 'No', 'Yes', 'Yes', 'No', 'No', 'No', 'No')
KS = data.frame(main_category, state)

cnt <- KS %>% group_by(main_category, state) %>% summarise(n=n())
pcnt <- do.call(rbind,
  lapply(split(cnt, cnt$main_category), function(x){x[x$state=='Yes', 'n']/sum(x$n)})
  )
names(pcnt) <- 'pcnt'
pcnt$main_category <- rownames(pcnt)
pcnt$state='Yes'
pcnt2 <- do.call(rbind,
                lapply(split(cnt, cnt$main_category), function(x){x[x$state=='No', 'n']/sum(x$n)})
)
names(pcnt2) <- 'pcnt'
pcnt2$main_category <- rownames(pcnt2)
pcnt2$state='No'
KS <- merge(KS, rbind(pcnt, pcnt2))

KS$labelpos <- ifelse(KS$state=='Yes',
                      KS$pcnt/2, 1 - KS$pcnt/2)


gg <- ggplot(data=KS, aes(x=main_category, fill=state)) 
gg <- gg + geom_bar(position="fill")
gg <- gg + geom_text(aes(label = paste0(100*pcnt,"%"),y=labelpos),size = 3)
gg <- gg + scale_y_continuous(labels = scales::percent)
print(gg)

这篇关于Ggplot填充的带有百分比标签的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆