我有一个包含以下变量的数据框:学生:Student1,student2 ... Ass_1_hearingE: [not tested, not fulfilled, partly fulfilled, fulfilled]
Ass_1_hearingC: [not tested, not fulfilled, partly fulfilled, fulfilled]
Ass_1_hearingA: [not tested, not fulfilled, partly fulfilled, fulfilled]
我要创建的图是在Y轴上学生的数量[854],在X轴上我想要变量HearingE,HearingC,HearingA和我想要绘制图表(或者可以显示什么百分比的东西)没有经过测试,没有实现,部分实现,满足。在每个基于听证会的学生中,听证会和听证会A)
听起来你正在寻找一个酒吧情节。如果你给出了实际数据的样本会更容易给你代码,但是,实际代码看起来像这样,使用ggplot2
:
require(tidyverse)
df<-df %>% gather(test, value, 2:4)
ggplot(df,aes(x=test,fill=value)) +
geom_bar(position="dodge")
请注意,关键是将数据转换为“长”格式 - 因此,不是每个测试都有一列,而是创建(使用gather
或cast
或一堆类似函数)两列 - 一个用于测试名称,以及值的名称。
例如,让我们创建一些随机数据:
df<-data.frame(student=c(LETTERS[1:10]), Ass_1_hearingE=sample(c("not tested",
"not fulfilled", "partly fulfilled", "fulfilled"),10,replace=TRUE),
Ass_1_hearingA=sample(c("not tested", "not fulfilled", "partly
fulfilled", "fulfilled"),10,replace=TRUE), Ass_1_hearingC =
sample(c("not tested", "not fulfilled", "partly fulfilled",
"fulfilled"),10,replace=TRUE))
这将给我们以下结果:
这是你想要的吗?
或者,如果你想把它作为堆积条,将上面的dodge
更改为stack
,你会得到: