制作一个三元情节 [英] Making a ternary plot
问题描述
我想使用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 (1,0,0,0,0,0,0),
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)
欢迎任何建议。非常感谢!
coord_trans
认为它的确如此。它会转换已经是二维的图的x和y坐标,但是你有三维数据。
只需自己转换数据,然后绘制它:
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))$ b $ (0,0,1,0.1,0.6,0.2),
x2 = c(0,1,...,b) 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 -喜欢。另外,你不应该在 aes()
中传递像
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!
coord_trans
doesn't do what you seem to think it does. It will transform the x and y coordinates of a plot that's already 2D, but you have 3D data.
Just transform the data yourself and then plot it:
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()
Note that I rewrote your transformation functions to be more R-like. Also, you shouldn't be passing expressions like x = c(x1,x2,x3)
inside of aes()
. You map a single variable in your data frame to a single aesthetic.
这篇关于制作一个三元情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!