Xavier Prudent - 1 year ago 71

R Question

I have three reference vectors

`a ( 0, 0, 1 )`

b ( 0, 1, 0 )

c ( 1, 0, 0 )

and will have measurements such as

`x( 0, 0.5, 0.3 )`

which I want to plot in a 2D figure as a triangle, who edges would correspond to a, b and c.

In Matlab there is a straighforward function to do that

`http://fr.mathworks.com/help/matlab/ref/triangulation.cartesiantobarycentric.html?s_tid=gn_loc_drop`

does anyone know an equivalent in R or should I implement the maths?

Answer Source

Sure, you can go back and forth between cartesian and barycentric.

Bary to Cart:

```
library(geometry)
## Define simplex in 2D (i.e. a triangle)
X <- rbind(
c( 0, 0, 1 ),
c( 0, 1, 0 ),
c( 1, 0, 0 ))
## Cartesian cooridinates of points
beta <- rbind(c( 0, 0.5, 0.3 ),
c(0.1, 0.8, 0.1),
c(0.1, 0.8, 0.1))
## Plot triangle and points
trimesh(rbind(1:3), X)
text(X[,1], X[,2], 1:3) # Label vertices
P <- bary2cart(X, beta)
```

Cart to Bary:

```
## Define simplex in 2D (i.e. a triangle)
X <- rbind(c(0, 0),
c(0, 1),
c(1, 0))
## Cartesian cooridinates of points
P <- rbind(c(0.5, 0.5),
c(0.1, 0.8))
## Plot triangle and points
trimesh(rbind(1:3), X)
text(X[,1], X[,2], 1:3) # Label vertices
points(P)
cart2bary(X, P)
```