我有以下代码:
dat <- read.table(text="Topic Project C10 C14 C03 C11 C16 C08
T1 P1 0.24 0.00 0.00 0.04 0.04 0.00
T2 P1 0.00 0.30 0.00 0.00 0.00 0.00
T3 P1 0.04 0.04 0.00 0.24 0.00 0.00
T4 P1 0.00 0.00 0.00 0.04 0.33 0.04
T5 P1 0.00 0.09 0.21 0.00 0.00 0.00
T6 P1 0.00 0.09 0.00 0.00 0.00 0.34
T1 P2 0.20 0.00 0.00 0.04 0.00 0.04
T2 P2 0.00 0.22 0.04 0.00 0.00 0.00
T3 P2 0.04 0.00 0.00 0.24 0.00 0.00
T4 P2 0.00 0.00 0.04 0.00 0.33 0.00
T5 P2 0.04 0.00 0.21 0.00 0.00 0.00
T6 P2 0.00 0.04 0.00 0.00 0.00 0.34",
header=TRUE)
layout(matrix(c(1,2,5,3,4,5),nrow=2,byrow = TRUE))
# [,1] [,2] [,3]
#[1,] 1 2 5
#[2,] 3 4 5
barcols <- c("red","blue","green","orange","black","yellow")
sapply(3:8,
function(x) {
bp <- barplot(matrix(dat[,x],nrow=2,byrow=TRUE),ylim=c(0, 0.5),beside=TRUE,col=barcols)
title(main=names(dat[x]))
axis(1,at=colMeans(bp),c("T1","T2","T3","T4","T5","T6"),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("C10","C10","C03","C11","C16","C08"),fill=barcols,cex=1.5)
The above code should plot two barplots using the side-by-side feature as follow:
不幸的是我只得到了一个图,这是不正确的,代码的灵感来自于此post https://stackoverflow.com/questions/21273691/how-to-create-2x2-bar-plot-with-side-by-side-pairwise-bar-in-r