所以我正在分析一个数据帧,其中包含学生(已退学),他们的学年(2020,2021,2022),以及他们是否接受了采访。我想以图形方式显示每个班级每年申请和实际接受面试的学生人数。
我尝试对此进行编码,但是在很多尝试中都收到了错误,这表明我可能无法正确处理此问题。
数据:
app <- structure(list(Interview = c("Yes", "Yes", "Yes", "Yes", "Yes",
"No", "Yes", "No", "No", "Yes", "No", "Yes", "No", "No", "Yes",
"Yes", "Yes", "Yes", "Yes", "No"), Year = c(2021, 2021, 2020,
2022, 2022, 2022, 2020, 2021, 2021, 2021, 2020, 2022, 2022, 2021,
2022, 2020, 2022, 2022, 2020, 2020)), row.names = c(NA, -20L), class =
c("tbl_df",
"tbl", "data.frame"))
然后我使用dplyr和ggplot对数据进行分组并相应地绘制 图书馆(dplyr) 库(ggplot2) 图书馆(ggthemes) 库(readxl)
year_table <- app %>%
group_by(Year) %>%
summarize(number = n()) %>%
mutate(pct=number/sum(number)) %>%
arrange(desc(pct))
year_table
#interview candidates
year_table_int <- app_int %>%
group_by(Year) %>%
summarize(number = n()) %>%
mutate(pct=number/sum(number)) %>%
arrange(desc(pct))
year_table
ggplot(data = year_table, mapping = aes(x = Year, y = number)) +
geom_col(fill= "darkslategray3") + theme_economist() +
ggtitle("Distribution of Applicants based on Class Year") +
geom_text(data=year_table, aes(label=paste0(round(pct*100,1),"%"),
y=number), size=4, vjust = -.5) +
labs(y = "Number of Applicants")
#Attempt 2
a<- 1:200
ggplot(year_table, aes(x=factor(Year), y=number)) +
geom_bar(position="dodge", stat="identity",aes(fill=factor(Interview))) +
coord_cartesian(ylim=c(50, 150)) + scale_y_continuous(breaks=a[a%%10==0]) +
xlab("Year") + ylab("Number of Applicants") +
scale_fill_discrete(name="Interview?") +
theme(axis.text.x = element_text(size=14))
在这一点上,我感到困惑,但我对自己的外观有一个视觉印象。也许使用geom_bar可能更好???不确定,无论我想在x轴上显示上课年份,还是在原因上显示数字(或计数),以及带阴影的横条,表示总申请人和接受采访的#人(采访=是)。