我目前正在尝试在R中的矩阵中绘制多个列。到目前为止,我已经解决了问题,但这是我的问题 - 当我提交一个包含5列的矩阵时,我只得到一个包含4行的图形。我注意到缺失的线总是最接近x轴的线。我现在已经工作了几个小时了,我尝试过几种不同的东西。任何关于如何让R产生第5条线的建议或帮助(相应的颜色填充x轴和线之间的空间)将不胜感激。
gender=cbind(matrix(malepop),matrix(femalepop))
plotmat(year,gender)
#a sample set
biggen=cbind(malepop,femalepop,malepop,femalepop)
#start of the function
plotmat2=function(years,m,colors){
n=m/1000000
#create a plot with the base line
plot(years,n[,1],type='l',ylim=c(0,10))
##create a for loop to generate all other lines and fill in the spaces
for (i in ncol(n):2) {
newpop=matrix(rowSums(n[,1:i]))
lines(year,newpop)
cord.xmat=c(min(years),years,max(years))
cord.ymat=c(-1,newpop[,1],-1)
polygon(cord.xmat,cord.ymat,col=clrs[i])
next
cord.xmat=c(min(years),years,max(years))
cord.ymat1=c(-1,n[,1]/1000000,-1)
polygon(cord.xmat,cord.ymat,col="purple")
}
}
#sample color set
clrs=c("red","blue","yellow","pink","purple", "cyan", "hotpink")
#run the function
plotmat2(year,biggen,clrs)
感谢您提供的任何和所有帮助!