1
我目前正在嘗試在R中的矩陣中繪製多列。到目前爲止,我已經計算出了一些東西,但這是我的問題 - 當我提交包含5列的矩陣時, 4行圖。我注意到,缺失的線總是最靠近x軸的線。我已經在這裏工作了好幾個小時,而且我嘗試了幾個不同的事情。我們將不勝感激任何關於如何讓R產生第5條線(相應的顏色填充X軸和線之間的空間)的建議或幫助。在R中繪製多個變量
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)
感謝您的任何和所有幫助您可以提供!