首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ggplot2中具有多个二进制变量的躲避条形图

Ggplot2中具有多个二进制变量的躲避条形图
EN

Stack Overflow用户
提问于 2019-02-09 03:27:22
回答 1查看 473关注 0票数 0

我想创建一个图,它显示了基于第三个变量(年份)的三个不同变量(总数,面试,雇用)中的赞成票计数。还值得注意的是,没有实际的总变量,而只是总的观察值

我正在尝试用ggplot2来做这件事,但是我尝试过的所有东西都没有产生我想要的结果。我可以使用geom_bar很容易地得到一个闪避和绘图,但我不确定如何表示两个不同的变量。

代码语言:javascript
运行
复制
 app <- structure(list(Applicant_Name = c("Aaraf", "Alaina", 
 "Aleena", "Alejandra", "Alexa", "Alexander", 
 "Alexandra", "Alexandra", "Alexandria", 
 "Alexis"), Interview = c("No", "No", "Yes", "Yes", "No", 
 "Yes", "Yes", "Yes", "Yes", "Yes"), Hire = c("No", "No", "Yes", 
 "No", "No", "No", "No", "No", "Yes", "Yes"), Year = c(2022, 2020, 
 2021, 2021, 2022, 2022, 2020, 2020, 2020, 2022), School = c("School of Business", 
 "Columbian Coll of Arts & Sci", "Milken Inst Sch of Public Hlth", 
 "Columbian Coll of Arts & Sci", "School of Engin & App Sc", "Columbian Coll of Arts & Sci", 
 "Columbian Coll of Arts & Sci", "Columbian Coll of Arts & Sci", 
 "School of Business", "Columbian Coll of Arts & Sci"), Major = c("Pre-Business Administration", 
 "Biological Anthropology", "Public Health", "Biological Anthropology", 
 "Systems Engineering", "Arts & Sciences", "Neuroscience", "English", 
 "International Business", "Arts & Sciences"), Ethnicity = c("Black or African American", 
 "White", "White", "Nonresident alien", "White", "White", "Race/ethnicity unknown", 
 "Two or More Race Codes", "Black or African American", "Black or African American"
 ), Sex = c("Female", "Female", "Female", "Female", "Female", 
 "Male", "Female", "Female", "Female", "Female"), GPA = c(3.221428, 
 3.230158, 3.429268, 3.576595, 3.86, 4, 3.460759, 3.89315, 3.227631, 
 1.433333)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
 "data.frame"))

 ggplot(app, aes(Year, ..count..)) + geom_bar(aes(fill = Hire), position = "dodge")

理想情况下,我希望在Hire=Yes总数旁边的Interview=Yes总数旁边显示我们的总申请者数量(所有观察结果),按年细分。

这是一个视觉例子,展示了我可爱的艺术能力。https://imgur.com/a/mGyzBfJ

EN

回答 1

Stack Overflow用户

发布于 2019-02-09 03:52:46

使用dplyrtidyr直接获取要绘制的数据:

代码语言:javascript
运行
复制
library(dplyr)
library(tidyr)
library(ggplot2)
app2 <- app %>% 
  group_by(Year) %>% 
  summarise(Total = n(),
            Interviewed = sum(Interview == "Yes"),
            Hired = sum(Hire == "Yes")) %>% 
  gather( "category", "counts", -Year)

然后就可以直接绘制了:

代码语言:javascript
运行
复制
ggplot(app2, aes(Year, counts)) + 
  geom_col(aes(fill = category), position = "dodge")

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54599014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档