Search code examples
rcompositionternaryrglsimplex

Plotting 4D compositional in a 3D tetrahedron in R


I would like to know how to connect-the-dots in the plot below.

I have four-variable compositional data, in which each row represents a sample, and each sample consists of varying proportions of four components (4 columns).

Reproducible example:

library(compositions); library(rgl)
TimeSeries <- cbind(runif(10),runif(10),runif(10),runif(10))
TimeSeries <- TimeSeries/rowSums(TimeSeries)
Acomp  <- acomp(TimeSeries)
plot3D(Acomp_TS,  cex=10, col="red",   log=FALSE, coors=T, bbox=F,    scale=F, center=F, axis.col=1, axes=TRUE)  

Ideally, I'd like to connect the dots in the order that they appear in the data frame.

I guess this might be accomplished with something like lines3d or segments3d (library rgl), but I can't see how to extract the (x,y,z) coordinates from Acomp.


Solution

  • You don't have a variable named Acomp_TS. I guess you meant Acomp.

    The best way to do this is to look at the source of plot3D.acomp, and do what it did. You might also want to suggest to the maintainer of the package that they should invisibly return the 3D coordinates that they computed to facilitate things like you want to do.

    But here's a hack that may work: after plotting the points, read their locations and use those as coordinates. For example,

    library(compositions); library(rgl)
    TimeSeries <- cbind(runif(10),runif(10),runif(10),runif(10))
    TimeSeries <- TimeSeries/rowSums(TimeSeries)
    Acomp  <- acomp(TimeSeries)
    plot3D(Acomp,  cex=10, col="red",   log=FALSE, coors=T, bbox=F,    scale=F, center=F, axis.col=1, axes=TRUE)
    ids <- rgl.ids()
    pts <- ids$id[ids$type == "points"]
    lines3d(rgl.attrib(pts, "vertices"))
    

    This produced

    enter image description here