R制作barplot

时间:2015-09-17 10:47:17

标签: r

我试图用以下数据制作一个条形图

      Dept
Admit        A   B   C   D   E   F
Admitted    601 370 322 269 147  46
Rejected    332 215 596 523 437 668

我尝试了以下代码:

admission_department <- barplot(biasUCB_d, main="Admit by deparment",
                                xlab="biasUCB_d[['Dept']]", 
                                col=c("darkblue","red"),
                                legend = rownames(biasUCB_d[['Dept']]), 
                                beside=TRUE)

用于创建数据集的编码名称为:

biasUCB_d <- margin.table(UCBAdmissions, c(1,3)) 

我做错了什么?

2 个答案:

答案 0 :(得分:0)

假设Dept是列表的元素,这应该可以工作:

数据:

biasUCB_d <- list(Dept = read.table(header=T, text='
Admit        A   B   C   D   E   F
Admitted    601 370 322 269 147  46
Rejected    332 215 596 523 437 668'))

解决方案:

dmission_department <- barplot(as.matrix(biasUCB_d$Dept[2:7]), main="Admit by deparment",
                               xlab="biasUCB_d[['Dept']]", 
                               col=c("darkblue","red"),
                               legend = biasUCB_d$Dept$Admit, 
                               beside=TRUE)

输出:

enter image description here

答案 1 :(得分:0)

尝试:

admission_department <- barplot(biasUCB_d, main="Admit by deparment",
                                xlab="Department", 
                                col=c("darkblue","red"),
                                legend.text = rownames(biasUCB_d), 
                                beside=TRUE)