我有这个data file有足够的数据点供我在三元图中绘制“热图”。 (它不是真正的热图,只是具有足够数据点的散点图)
library(ggtern)
library(reshape2)
N=90
trans.prob = as.matrix(read.table("./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
ggtern(data = flattened.tb, aes(x=x,y=y,z=z)) +
geom_point(size=1, aes(color=W12)) +
theme_bw() +
scale_color_gradient2(low = "green", mid = "yellow", high = "red")
这是我得到的:
我希望使用ggtern
获得类似以下内容:
我的问题是:如何使用ggtern
获得第二个数字?
编辑1 :抱歉文件名中有拼写错误。我修改了文件名。 数据文件包含太多数据点,我可以直接将它们粘贴到这里。
第二个数字是由第三方Matlab软件包ternplot
生成的。我想要一个三元等高线图,它在我的第一个图中有离散线而不是热图。更具体地说,我想指定一个轮廓线列表,例如W12=0.05,0.1,0.15,...
。我和geom_density_tern
和geom_interpolate_tern
玩了好几个小时,但仍然不知道如何得到我想要的东西。
MATLAB代码是:
[HCl, Hha, cax] = terncontour(X,Y,1-X-Y,data,[0.01,0.1,0.2,0.3,0.4,0.5]);
其中X,Y,1-X-Y
指定绘图上的坐标,data
存储值,矢量指定轮廓的值。
答案 0 :(得分:4)
这看起来并不像你的例子那么漂亮,但希望它能让你更接近你想要的地方:
flattened.tb$a <- 0
flattened.tb$a[flattened.tb$W12 > 0.04 & flattened.tb$W12 < .05] <- 1
flattened.tb$b <- 0
flattened.tb$b[flattened.tb$W12 > 0.05 & flattened.tb$W12 < .06] <- 1
flattened.tb$c <- 0
flattened.tb$c[flattened.tb$W12 > 0.07 & flattened.tb$W12 < .08] <- 1
flattened.tb$d <- 0
flattened.tb$d[flattened.tb$W12 > 0.09 & flattened.tb$W12 < .1] <- 1
options("tern.discard.external" = F)
ggtern(data = flattened.tb, aes(x, y, z)) +
geom_line(aes(a),color="red",linetype=1) +
geom_line(aes(b),color="blue",linetype=1) +
geom_line(aes(c),color="yellow",linetype=1) +
geom_line(aes(d),color="green",linetype=1) +
theme_bw()
情节只是需要一个漂亮的。我不能说哪些数据区域最适合绘图。
答案 1 :(得分:4)
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()
这会产生以下输出: