R:用ggtern 2.2.1填充等高线图

时间:2017-08-18 17:02:59

标签: r ggtern

我正在尝试用ggtern 2.2.1复制这个Minitab三元图。

Minitab Ternary Contour Plot

我的R代码如下:

myData<-data.frame(x=c(0.00000, 0.16667, 0.66667, 0.16667, 0.50000, 0.50000, 
                       1.00000, 0.00000, 0.00000, 0.33333),
                   y=c(0.00000, 0.16667, 0.16667, 0.66667, 0.00000, 0.50000, 
                       0.00000, 1.00000, 0.50000, 0.33333),
                   z=c(1.00000, 0.66667, 0.16667, 0.16667, 0.50000, 0.00000, 
                       0.00000, 0.00000, 0.50000, 0.33333),
                   a=c(0.546, 0.550, 0.455, 0.658, 0.451, 0.559, 0.355, 
                       0.762, 0.654, 0.554))

myData$a_breaks<-cut(myData$a, 
                     breaks=seq(0.4, 0.7, 0.1), include.lowest = TRUE)

ggtern(myData, aes(y, x, z))+
  stat_interpolate_tern(aes(value=a, fill=..level..), 
                        geom="polygon",
                        method="lm", 
                        binwidth=0.1, 
                        expand=1, 
                        base="identity")

我已经复制了轮廓线(见下文),但无法填充它们。我也收到了消息

Warning message:
In structure(c(), class = c(class(x), class(y))) :  
  Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.

关于我做错的任何指导?

ggtern contour plot

1 个答案:

答案 0 :(得分:0)

通过使用this question的答案,我能够得到一些近似于我所寻找的东西,但它仍然不完全相同 - 例如,轮廓之间的划分看起来非常粗糙。

library(ggplot2) 
library(ggtern) 

x<-seq(0,1,0.01)
y<-seq(0,1,0.01)

myData<-expand.grid(x=x,y=y)

myData$z<-1 - (myData$x + myData$y)
myData<-myData[myData$z>(-0.01),]
myData$a<-(0.355*myData$x + 0.762*myData$y + 0.546*myData$z)
myData$a_breaks<-cut(myData$a, breaks=seq(0.3, 0.8, 0.1), include.lowest =         
                     TRUE)

ggtern(myData, aes(y, x, z, color=a_breaks))+
  geom_point(size=5)+
  scale_color_brewer(type="seq", palette = "Blues")

R ternary contour plot