如何创建2x2条形图-在R中使用并排成对条形图
我有以下数据:如何创建2x2条形图-在R中使用并排成对条形图,r,plot,R,Plot,我有以下数据: Method Metric E0 E1 E2 E4 1 M1 Precision 0.9661017 0.9622642 1.0000000 0.9655172 2 M2 Precision 0.5330000 0.5350000 0.3780000 0.2140000 3 M1 Recall 0.9736842 0.9736842 0.9473684 0.9473684 4
Method Metric E0 E1 E2 E4
1 M1 Precision 0.9661017 0.9622642 1.0000000 0.9655172
2 M2 Precision 0.5330000 0.5350000 0.3780000 0.2140000
3 M1 Recall 0.9736842 0.9736842 0.9473684 0.9473684
4 M2 Recall 1.0000000 1.0000000 1.0000000 0.6670000
我要做的是将上述数据创建到以下绘图中:
在这个草图中,横杆的高度肯定不能反映上述数据
怎么做?
无需使用外部库,如ggplot或lattice。这里是:
进口:
dat <- read.table(text="Method Metric E0 E1 E2 E4
M1 Precision 0.9661017 0.9622642 1 0.9655172
M2 Precision 0.533 0.535 0.378 0.214
M1 Recall 0.9736842 0.9736842 0.9473684 0.9473684
M2 Recall 1 1 1 0.667",header=TRUE)
结果:
我知道您正试图避免使用外部库,但
ggplot
的设计目的就是为了简化这一过程
library(ggplot2)
library(reshape2)
gg <- melt(df,id=1:2)
ggplot(gg) +
geom_bar(aes(x=Method, y=value, fill=Metric), stat="identity",
position="dodge")+facet_wrap(~variable)
库(ggplot2)
图书馆(E2)
非常感谢。你介意看一下后续问题吗?
sapply(3:6,
function(x) {
bp <- barplot(matrix(dat[,x],nrow=2,byrow=TRUE),beside=TRUE,col=barcols)
title(main=names(dat[x]))
axis(1,at=colMeans(bp),c("M1","M2"),lwd=0,lwd.tick=1)
abline(h=0)
}
)
plot(NA,xlim=c(0,1),ylim=c(0,1),ann=FALSE,axes=FALSE)
legend(0,0.6,c("Precision","Recall"),fill=barcols,cex=1.5)
library(ggplot2)
library(reshape2)
gg <- melt(df,id=1:2)
ggplot(gg) +
geom_bar(aes(x=Method, y=value, fill=Metric), stat="identity",
position="dodge")+facet_wrap(~variable)