ggplot2使用... prop ...并按另一类别对条形图分组的问题 [英] ggplot2 problems with using ...prop... and grouping bar graph by another category

查看:58
本文介绍了ggplot2使用... prop ...并按另一类别对条形图分组的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StudentData <- data.frame(gender = sample( c("male","female"), 100, replace=TRUE),
              degree = sample( c("Associates", "Masters", "PhD"), 100, replace=TRUE),
              category = sample( c("Audit", "Credit"), 100, replace=TRUE))

在以下数据集中,我试图创建一个条形图,以图表方式绘制具有性别,性别(使用facet_grid()完成)的具有助理,硕士或博士学位的样本所占的百分比.到目前为止,这是我生成的:

In the following dataset, I am trying to create a bar graph that plots the percentage of the sample which have an Associate's, Master's, or PhD, separated by gender (done by using facet_grid() ). This is what I have generated so far:

StudentData %>% ggplot(., aes(x=degree, group=gender)) + 
            geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
            geom_text(aes(label=scales::percent(round(..prop..,2)), 
            y=..prop..), stat="count", vjust=-.5) +
            scale_y_continuous(limits=c(0,1),labels = scales::percent) +
            ylab("Percent of Sample") +
            facet_grid(~gender)

但是,我还想在每张图表上以条形显示审核"和信用"组之间的差异.但是,当我在ggplot的美学中添加"fill = category"时,没有任何变化:

However, I would also like to display the difference between the groups "Audit" and "Credit" on each graph as side by bars. Yet, when I add "fill=category" to the aesthetics of ggplot, nothing changes:

StudentData %>% ggplot(., aes(x=degree, group=gender, fill=category)) + 
            geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
            geom_text(aes(label=scales::percent(round(..prop..,2)), 
            y=..prop..), stat="count", vjust=-.5) +
            scale_y_continuous(limits=c(0,1),labels = scales::percent) +
            ylab("Percent of Sample") +
            facet_grid(~gender)

我意识到通常这是通过 geom_bar(stat ="identity",position = position_dodge())完成的,但是当我更改 stat ="identity" 时,出现以下错误消息:

I realize that usually this is accomplished using geom_bar(stat="identity", position=position_dodge()) but when I change stat="identity", the following error message appears:

FUN(X [[i]],...)中的错误:找不到对象'prop'

有什么想法可以使用构面图,使用特殊字符(例如..prop ..)并为ggplot2图添加其他填充吗?

Any idea how to have a facet graph, use special characters such as ..prop.. AND add another fill to a ggplot2 graph?

推荐答案

您可以像这样创建必要的四个 group s(而不是两个):

You could create the necessary four groups (instead of two) like this:

StudentData %>% 
  ggplot(., aes(x=degree, group=interaction(gender, category), fill=category)) + 
  geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
  geom_text(aes(label=scales::percent(round(..prop..,2)), 
                y=..prop..), stat="count", vjust=-.5, position=position_dodge(.9)) +
  scale_y_continuous(limits=c(0,1),labels = scales::percent) +
  ylab("Percent of Sample") +
  facet_grid(~gender) 

这篇关于ggplot2使用... prop ...并按另一类别对条形图分组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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