[R] Question of programming style
Uwe Ligges
ligges at statistik.uni-dortmund.de
Sat Jul 8 13:06:57 CEST 2000
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list