Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

r - Making a ternary plot

I want to plot the projection of 3-dimensional data on their simplex using ggplot2. I thought I could manage the transformation on cartesian coordinates using coord_trans(), but do not know how to do it exactly.

This is what I tried:

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

Any suggestions are appreciated. Many thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As mmann1123 highlighted, using ggtern, the following can be achieved:

Output

With the following simple codeblock:

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(x2,x1,x3)) + 
   geom_mask() +
   geom_point(fill="red",shape=21,size=4) + 
   theme_bw() +
   theme_showarrows() +
   theme_clockwise()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...