制作一个三元相图(Making a ternary plot)

2019-06-24 04:03发布

我想绘制的三维数据的投影使用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" )

任何建议表示赞赏。 非常感谢!

Answer 1:

coord_trans不会做你仿佛觉得它。 这将改变x和曲线这已经2D 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() 你在你的数据帧中的单个变量映射到一个单一的审美。



Answer 2:

作为mmann1123强调,使用ggtern ,下面就可以实现:

用下面简单的代码块:

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


Answer 3:

在VCD包装的ternaryplot功能确实使非规范化的数据古典三元地块了很好的工作:

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



Answer 4:

使用您可以在这里读到它的ggtern库。 http://ggtern.com/



Answer 5:

将R包三元产生从使用标准的图形函数矩阵和data.frames三元曲线图。

上述情节与创建:

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


文章来源: Making a ternary plot
标签: r ggplot2