Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。












想改善这个问题吗?更新问题,以使为on-topic

2年前关闭。



Improve this question





我的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")


这是我得到的:

r - R中的ggtern等高线图-LMLPHP

我想使用ggtern获得类似以下的内容:

r - R中的ggtern等高线图-LMLPHP

我的问题是:如何使用ggtern获得类似第二个数字的东西?

编辑1:对不起文件名中的错字。我修复了文件名。
数据文件包含太多数据点,我无法直接将其粘贴到此处。

第二个图形是由​​第三方Matlab软件包ternplot生成的。我想要一个具有离散线的三元轮廓图,而不是第一张图中的热图。更具体地说,我想指定轮廓线列表,例如W12=0.05,0.1,0.15,...。我已经在geom_density_terngeom_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存储值,向量指定轮廓值。

最佳答案

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


这将产生以下输出:

r - R中的ggtern等高线图-LMLPHP

为了生成更接近颜色和方向的图表作为示例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()


这将产生以下输出:

r - R中的ggtern等高线图-LMLPHP

08-24 14:37