WDG,我对 ggtern 做了一些小改动,以便更好地处理这种类型的建模,它刚刚提交给 CRAN,所以应该在第二天左右可用。在此期间,您可以从我的 BitBucket 帐户的源代码下载:https ://bitbucket.org/nicholasehamilton/ggtern
无论如何,这里是源代码,它适用于 ggtern 版本 2.1.2。
我已经包含了下面的点(具有温和的 alpha 值),因此可以观察到插值几何的代表性:
library(ggtern)
library(reshape2)
N=90
trans.prob = as.matrix(read.table("~/Downloads/N90_p_0.350_eta_90_W12.dat",fill=TRUE))
colnames(trans.prob) = NULL
# flatten trans.prob for ternary plot
flattened.tb = melt(trans.prob,varnames = c("x","y"),value.name = "W12")
# delete rows with NA
flattened.tb = flattened.tb[complete.cases(flattened.tb),]
flattened.tb$x = (flattened.tb$x-1)/N
flattened.tb$y = (flattened.tb$y-1)/N
flattened.tb$z = 1 - flattened.tb$x - flattened.tb$y
############### MODIFIED CODE BELOW ###############
#Remove the (trivially) Negative Concentrations
flattened.tb = subset(flattened.tb,z >= 0)
#Plot a series of plots in increasing polynomial degree
plots = lapply(seq(3,18,by=3),function(x){
degree = x
breaks = seq(0.025,0.575,length.out = 10)
base = ggtern(data = flattened.tb, aes(x=x,y=y,z=z)) +
geom_point(size=1, aes(color=W12),alpha=0.05) +
geom_interpolate_tern(aes(value=W12,color=..level..),
base = 'identity',method = glm,
formula = value ~ polym(x,y,degree = degree,raw=T),
n = 150, breaks = breaks) +
theme_bw() +
theme_legend_position('topleft') +
scale_color_gradient2(low = "green", mid = "yellow", high = "red",
midpoint = mean(range(flattened.tb$W12)))+
labs(title=sprintf("Polynomial Degree %s",degree))
base
})
#Arrange the plots using grid.arrange
png("~/Desktop/output.png",width=700,height=900)
grid.arrange(grobs = plots,ncol=2)
garbage <- dev.off()
这会产生以下输出:
为了生成更接近颜色和方向的图表作为示例 matlab 等高线图,请尝试以下操作:
plots = lapply(seq(3,18,by=3),function(x){
degree = x
breaks = seq(0.025,0.575,length.out = 10)
base = ggtern(data = flattened.tb, aes(x=z,y=y,z=x)) +
geom_point(size=1, aes(color=W12),alpha=0.05) +
geom_interpolate_tern(aes(value=W12,color=..level..),
base = 'identity',method = glm,
formula = value ~ polym(x,y,degree = degree,raw=T),
n = 150, breaks = breaks) +
theme_bw() +
theme_legend_position('topleft') +
scale_color_gradient2(low = "darkblue", mid = "green", high = "darkred",
midpoint = mean(range(flattened.tb$W12)))+
labs(title=sprintf("Polynomial Degree %s",degree))
base
})
png("~/Desktop/output2.png",width=700,height=900)
grid.arrange(grobs = plots,ncol=2)
garbage <- dev.off()
这会产生以下输出: