標準barplot也不錯,你只需要把你的數據放在一個矩陣中。例如:
L <- data.frame(point = c(3, 5, 6, 7, 8, 9, 11),
layer1 = c(1, 25, 20, 90, 80, 100, 45),
layer2 = c(5, 20, 0, 0, 0, 70, 0),
layer3 = c(80, 5, 0, 0, 0, 0, 0))
barplot(t(as.matrix(L)),col=c("blue","black","yellow","orange"))
做的正是你想要的東西意味着循環:
L <- data.frame(
layer1 = c(25, 20, 90, 80, 100, 45),
layer2 = c(5, 20, 0, 0, 70, 0),
layer3 = c(80, 5, 0, 0, 0, 0))
rownames(L)<-c(3, 5, 6, 7, 8, 9)
AL_col <- matrix(c(
4,3,3,3,3,3,
5,5,6,6,6,5,
4,3,6,6,6,6),ncol=6,byrow=TRUE)
colnames(AL_col) <- c("point3","point5","point6","point7","point8","point9")
rownames(AL_col) <- c("layer1","layer2","layer3")
col1<-c("green","brown","purple","black","yellow", "white","blue")
# the problem is then to make polygons corresponding to your colors
maxHeight <- max(as.matrix(L) %*% rep(1,dim(L)[2]))
widthPol <- 0.5
plot(c(1-widthPol,dim(L)[1]+widthPol),c(0,maxHeight),type="n",xlab="Points",ylab="Height")
for(iPoint in 1:6){
currentY <- 0
for(iLayer in 1:3){
addedY <- L[iPoint,iLayer]
if(addedY>0){
xs <- c(rep(iPoint-widthPol/2,2),rep(iPoint+widthPol/2,2))
ys <- c(0,addedY,addedY,0)
colPoly <- col1[AL_col[iLayer,iPoint]]
polygon(x=xs,y=ys+currentY,col=colPoly)
currentY <- currentY + addedY
}
}
}
在您的AL的定義點的數量不匹配的數量列。你能發佈生成AL和AL_col的R代碼嗎? – cmbarbu 2015-02-24 14:35:36