我想创建一个图,它显示了基于第三个变量(年份)的三个不同变量(总数,面试,雇用)中的赞成票计数。还值得注意的是,没有实际的总变量,而只是总的观察值
我正在尝试用ggplot2来做这件事,但是我尝试过的所有东西都没有产生我想要的结果。我可以使用geom_bar很容易地得到一个闪避和绘图,但我不确定如何表示两个不同的变量。
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
发布于 2019-02-09 03:52:46
使用dplyr
和tidyr
直接获取要绘制的数据:
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)
然后就可以直接绘制了:
ggplot(app2, aes(Year, counts)) +
geom_col(aes(fill = category), position = "dodge")
https://stackoverflow.com/questions/54599014
复制相似问题