在R中绘制多个变量
我目前正在尝试在R中绘制矩阵中的多列。到目前为止,我已经解决了一些问题,但这是我的问题-当我提交一个包含5列的矩阵时,我只得到一个包含4行的图。我注意到丢失的线总是离x轴最近的线。我已经为此工作了好几个小时了,我尝试了几种不同的方法。对于如何让R生成第五条线(x轴和线之间填充相应颜色)的任何建议或帮助,我们将不胜感激在R中绘制多个变量,r,graph,R,Graph,我目前正在尝试在R中绘制矩阵中的多列。到目前为止,我已经解决了一些问题,但这是我的问题-当我提交一个包含5列的矩阵时,我只得到一个包含4行的图。我注意到丢失的线总是离x轴最近的线。我已经为此工作了好几个小时了,我尝试了几种不同的方法。对于如何让R生成第五条线(x轴和线之间填充相应颜色)的任何建议或帮助,我们将不胜感激 gender=cbind(matrix(malepop),matrix(femalepop)) plotmat(year,gender) #a sampl
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)
感谢您提供的一切帮助 可能是您无意中用其他彩色部分覆盖了第一行,并且您可能跳过了n[,1]多边形的创建 从您尝试以降序绘制列的方式来看,我假设您知道列的大小顺序是升序的(示例图中粉红色的部分将是矩阵“biggen”中的最后一列)。如果我在这一点上错了,那么使用“密度”参数更改多边形着色可能是一个好主意,这可以帮助您查看是否意外地覆盖了其他部分
## plotmat2 function
plotmat2=function(years,m,colors){
n=m/1000000
#create a blank plot based on the baseline
plot(years,n[,1],type='n',ylim=c(0,10))
##create a for loop to generate all other lines and fill in the spaces
for (i in ncol(n):1) {
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=colors[i], density=10)
}
}
另外,如果这无助于解决问题,那么如果您提供了数据集的一部分,可能会有所帮助。我仍在学习R和StackOverflow,但这似乎是我在这里读到的许多线程中给出的明智建议。祝你好运