2012-05-03 15 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パッケージをインストールする方法]も参照してください。 install-ggtern-package-in-r) – Dante

答えて

1

coord_transあなたが思っていると思われることはしません。すでに2Dのプロットのx座標とy座標を変換しますが、3Dデータがあります。

ただ、データを自分で変換し、それをプロットします。私はより多くのR-ようであるためにあなたの変換関数を書き直し

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

注意。また、x = c(x1,x2,x3)のような式をaes()の中に入れてはいけません。データフレーム内の1つの変数を1つの美学にマッピングします。

3

ternaryplot機能VCDパッケージは、非正規化されたデータから、古典的な三元プロットを作るの素晴らしい仕事をしていませんで:

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

enter image description here

2

あなたはここでそれについて読むことができますggternライブラリを使用してください。 http://ggtern.com/

12

mmann1123は、以下を達成することができる、ggternを使用して、強調表示されたとおりの三元生成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) 
) 
ggtern(data=x,aes(x1,x2,x3)) + 
    geom_point(fill="red",shape=21,size=4) + 
    theme_tern_bw() 
0

Rパッケージ:次の簡単なコードブロックと

Output

を行列とデータからフレームをプロットする。標準的なグラフィックス機能。

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