我有一个包含以下变量的数据框:学生:学生1,学生2 …Ass_1_hearingE:[未测试,未完成,部分完成,已完成]Ass_1_hearingC:[未测试,未完成,……
听起来你正在寻找一个酒吧情节。如果您提供了实际数据的样本,那么更容易为您提供代码,但是,实际代码看起来像这样,使用 ggplot2 :
ggplot2
require(tidyverse) df<-df %>% gather(test, value, 2:4) ggplot(df,aes(x=test,fill=value)) + geom_bar(position="dodge")
请注意,关键是将数据转换为“长”格式 - 因此,您不必为每个测试创建一个列,而是创建(使用 gather , 要么 cast 或者一堆类似的函数)两列 - 一列用于测试名称,一列用于值。
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))
这将给我们以下结果:
这是你想要的吗?
或者,如果你想要它作为堆叠条,将上面的“闪避”更改为“堆叠”,你得到这个: