2012-05-03 108 views
9

我想繪製的三維數據的投影使用GGPLOT2他們的單純。我以爲我可以管理使用coord_trans()直角座標系的轉換,但不知道如何做到這一點沒錯。製作三元相圖

這是我的嘗試:

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") 

任何建議表示讚賞。非常感謝!

+0

參見(http://askubuntu.com/questions/608519/how-to- [如何R中安裝ggtern包]安裝-ggtern封裝式-R) – Dante

回答

1

coord_trans不會做你彷彿覺得它。它將變換已經是2D的圖的x和y座標,但是你有3D數據。

只是自己變換的數據,然後繪製它:

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() 

請注意,我改寫了你的轉換功能更加R-等。此外,你不應該通過這樣的表達式x = c(x1,x2,x3)內的aes()。您可以將數據框中的單個變量映射爲單一的審美。

3

在VCD包裝的ternaryplot函數做非標準化的數據製作的經典三元地塊了很好的工作:

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

enter image description here

12

作爲mmann1123突出顯示,使用ggtern,以下就可以實現:

Output

用下面的簡單的碼塊:

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

將R包Ternary產生三元用矩陣和data.frames繪製圖標準圖形功能。

Ternary plot created with R package Ternary

上述情節將創建具有:

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')