This is really a question of how to program this *BETTER*. It works as I have done it, but is quite ugly. I want to do a 3d scatterplot of the upper triangle of a matrix, where the z-values are the values in the matrix, and the row and column indices are the y- and x-values. The complete (11 by 11) matrix is mmtop94.2. Here is my awkward code: mmtop94.2[lower.tri(mmtop94.2)] <- NA # Here i is the row (y variable), j is the column (x var) plotdata <- matrix(0, ncol=3, nrow=121) for (i in 1:11) { for (j in 1:11) { k <- (i-1) * 11 + j plotdata[k,] <- c(j, abs(i - 12), mmtop94.2[i, j]) } } plotdata2 <- na.omit(plotdata) scatterplot3d(plotdata2, type='h') It seems to me that I should not have to initialize the plotdata matrix. And are all those for loops really necessary? Thanks in advance for the help. ______________________________________________________________________ Stuart Luppescu -=-=- University of Chicago $(B:MJ8$HCRF`H~$NIc(B -=-=- s-luppescu at uchicago.edu http://www.consortium-chicago.org/people/sl/sl.html PGP Public Key: www.consortium-chicago.org/people/sl/pubkey.asc ICQ #21172047 AIM: psycho7070 Swipple's Rule of Order: He who shouts the loudest has the floor.>> Sent on 07-Jul-2000 at 17:45:09 with xfmail-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I want to do a 3d scatterplot of the upper triangle of a matrix, where the z-values are the values in the matrix, and the row and column indices are the y- and x-values. The complete (11 by 11) matrix is mmtop94.2. Here is my awkward code: mmtop94.2[lower.tri(mmtop94.2)] <- NA # Here i is the row (y variable), j is the column (x var) plotdata <- matrix(0, ncol=3, nrow=121) for (i in 1:11) { for (j in 1:11) { k <- (i-1) * 11 + j plotdata[k,] <- c(j, abs(i - 12), mmtop94.2[i, j]) } } plotdata2 <- na.omit(plotdata) scatterplot3d(plotdata2, type='h') It seems to me that I should not have to initialize the plotdata matrix. And are all those for loops really necessary? I remember doing similar things sometime ago simply by filling the unwanted triangle of z with NA's. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
s-luppescu at uchicago.edu wrote:> > This is really a question of how to program this *BETTER*. It works as I have > done it, but is quite ugly. > > I want to do a 3d scatterplot of the upper triangle of a matrix, where the > z-values are the values in the matrix, and the row and column indices are the y- > and x-values. The complete (11 by 11) matrix is mmtop94.2. Here is my awkward > code: > > mmtop94.2[lower.tri(mmtop94.2)] <- NA > > # Here i is the row (y variable), j is the column (x var) > plotdata <- matrix(0, ncol=3, nrow=121) > for (i in 1:11) > { > for (j in 1:11) > { > k <- (i-1) * 11 + j > plotdata[k,] <- c(j, abs(i - 12), mmtop94.2[i, j]) > } > } > plotdata2 <- na.omit(plotdata) > > scatterplot3d(plotdata2, type='h')You only need to specify the points to be plotted: ## upper triangle of the correct size: temp <- upper.tri(mmtop94.2, diag = TRUE) z <- mmtop94.2[temp] # values y <- nrow(mmtop94.2) + 1 - row(mmtop94.2)[temp] # row indices x <- col(mmtop94.2)[temp] # col indices scatterplot3d(x, y, z, type="h")> It seems to me that I should not have to initialize the plotdata matrix. And > are all those for loops really necessary?Right. You'll need a "plotdata matrix" only for surface plots (e.g. persp(.)). scatterplot3d(.) is designed to plot 3D point clouds. Regards, Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._