2012-05-03 52 views
9

Quiero trazar la proyección de datos tridimensionales en su símplex utilizando ggplot2. Pensé que podría gestionar la transformación en coordenadas cartesianas usando coord_trans(), pero no sé cómo hacerlo exactamente.Realización de un gráfico ternario

Esto es lo que he intentado:

simplex.y <- function(x1, x2, x3) { 
    return(sqrt(0.75) * x3/(x1+x2+x3)) 
} 
simplex.x <- function(x1, x2, x3) { 
    return((x2 + 0.5 * x3)/(x1+x2+x3)) 
} 

x <- data.frame(
    x1 = c(0, 0, 1, 0.1, 0.6, 0.2), 
    x2 = c(0, 1, 0, 0.3, 0.2, 0.8), 
    x3 = c(1, 0, 0, 0.6, 0.2, 0.0) 
) 

require(ggplot2) 
ggplot(data = x, aes(x = c(x1, x2, x3), y = c(x1, x2, x3))) + 
    geom_point() + 
    coord_trans(x="simplex.x", y="simplex.y") 

Se aprecia cualquier sugerencia. ¡Muchas gracias!

+0

Véase también [Cómo instalar el paquete de ggtern en I] (http://askubuntu.com/questions/608519/how-to- install-ggtern-package-in-r) – Dante

Respuesta

1

coord_trans no hace lo que parece pensar que hace. Transformará las coordenadas xey de una gráfica que ya es 2D, pero tiene datos en 3D.

Sólo transformar los datos a sí mismo y luego graficar que:

simplex.y <- function(x) { 
    return(sqrt(0.75) * x[3]/sum(x)) 
} 
simplex.x <- function(x) { 
    return((x[2] + 0.5 * x[3])/sum(x)) 
} 

x <- data.frame(
    x1 = c(0, 0, 1, 0.1, 0.6, 0.2), 
    x2 = c(0, 1, 0, 0.3, 0.2, 0.8), 
    x3 = c(1, 0, 0, 0.6, 0.2, 0.0) 
) 

newDat <- data.frame(x = apply(x,1,simplex.x), 
       y = apply(x,1,simplex.y)) 

ggplot(newDat,aes(x = x,y = y)) + 
    geom_point() 

Tenga en cuenta que Reescribí sus funciones de transformación para ser más R-similares. Además, no debería estar pasando expresiones como x = c(x1,x2,x3) dentro de aes(). Asigna una única variable en su marco de datos a una única estética.

3

La función ternaryplot en el paquete VCD hace un buen trabajo de hacer diagrama ternario clásicos a partir de datos no normalizada:

require(vcd) 
#ternaryplot takes matrices but not data frames 
xM <- as.matrix(x) 
ternaryplot(xM) 

enter image description here

12

Como mmann1123 resaltado, usando ggtern, se puede lograr el siguiente:

Output

Con el siguiente bloque de código simple:

x <- data.frame(
    x1 = c(0, 0, 1, 0.1, 0.6, 0.2), 
    x2 = c(0, 1, 0, 0.3, 0.2, 0.8), 
    x3 = c(1, 0, 0, 0.6, 0.2, 0.0) 
) 
ggtern(data=x,aes(x1,x2,x3)) + 
    geom_point(fill="red",shape=21,size=4) + 
    theme_tern_bw() 
0

El paquete R Ternary produce ternario parcelas de matrices y data.frames utilizando el funciones gráficas estándar.

Ternary plot created with R package Ternary

El gráfico de arriba se crea con:

x <- data.frame(
    x1 = c(0, 0, 1, 0.1, 0.6, 0.2), 
    x2 = c(0, 1, 0, 0.3, 0.2, 0.8), 
    x3 = c(1, 0, 0, 0.6, 0.2, 0.0) 
) 
TernaryPlot() 
TernaryPoints(x, col='red') 
Cuestiones relacionadas