用于r中多边形绘图的颜色和坐标矩阵对象的循环

时间:2012-07-30 17:31:04

标签: r loops colors polygon

这是多边形坐标:

#data

greenM <-matrix(c(5,5,95,45,5,65,5,45,65,5,5,95),nrow=4,byrow=TRUE)
blueM <-matrix(c(35,15,65,35,45,35,5,75,35,5,45,65,35,15,65),
 nrow=5,byrow=TRUE)
purpleM <-matrix(c(95,5,15,95,5,5,35,75,5,35,45,35,55,45,15,95,5,15),
  nrow=6,byrow=TRUE)
redM <-matrix(c(35,45,35,35,75,5,5,95,5,5,75,35,35,45,35),
 nrow=5,byrow=TRUE)
yellowM <-matrix(c(95,5,15,45,5,65,35,15,65,35,45,35,55,45,
15,95,5,15),nrow=6,byrow=TRUE)

这是绘图(unlooped,但我希望用短代码自动化长代码,以便可以使用x个颜色生成x个绘图)

plot(NA,NA,xlim=c(0,10),ylim=c(0,100),asp=1,bty="n",axes=F,xlab="",ylab="")
polygon(greenM,col="green",border=NULL)
polygon(blueM,col="blue",border=NULL)
polygon(purpleM,col="purple",border=NULL)
polygon(redM,col="red",border=NULL)
polygon(yellowM,col="yellow",border=NULL)

如何创建一个外观,它将按顺序应用于不同颜色的坐标列表。

mycolist <- list (greenM, blueM, purpleM, redM, yellowM)
col1 <- c("green", "blue", "purple", "red", "yellow")

1 个答案:

答案 0 :(得分:2)

查看mapply

mycolist <- list (greenM, blueM, purpleM, redM, yellowM)
col1 <- c("green", "blue", "purple", "red", "yellow")
plot(NA,NA,xlim=c(0,10),ylim=c(0,100),asp=1,bty="n",axes=F,xlab="",ylab="")
mapply(polygon,x=mycolist,col=col1)

如果您不希望所有NULL输出到屏幕,只需将其包装在invisible

invisible(mapply(polygon,x=mycolist,col=col1))