如何在R中创建三元热图?

kadbb459  于 2023-03-15  发布在  其他
关注(0)|答案(2)|浏览(193)

我想做一个三角形图来表示XYZ因子所有可能组合的响应面,三角形内的梯度区域表示响应变量Gi,圆点表示X的10个组合, Dataframe dt中的YZGi与(XYZ)之间的相关性定义为mdl <- lm (Gi ~ X*Y*Z),下面是数据和我的尝试:

X <- rep(c(.45,.4,.55,.4,.43,.5,.43,.5,.43,.48), each = 3)
Y <- rep(c(.15, .12,.22,.14,.14,.19,.12, .17,.17,.12 ), each = 3)
Z <- rep(c(.15,.22,.12,.12,.19,.14,.14,.17,.12,.17), each = 3)
Gi <- c(353,381,320,312,335,265,394,350,374,320,299,316,300,304,295,360,331,395,351,280,342,299,303,279,374,364,419,306,290,315)

dt <- data.frame (X, Y, Z, Gi)
ggtern(data = dt, aes(x = X, y = Y, z = Z, value = Gi)) +
  stat_interpolate_tern(geom="polygon",
                        formula = value ~ x+y, 
                        method = lm,
                        aes(fill = ..level..), expand = 1) +
  scale_fill_gradient(low="green", high="blue") +
  geom_point (fill = "white", size = 3, shape = 21, color = "white") +
  theme_gray () +
  theme ( tern.axis.arrow.show = T)

然而,输出并不是我想要的,我发现了一个令人兴奋的使用Python的三元图热图的例子,这就像我想要的,然而,我只熟悉R,我想做一些类似的东西,我怎么能在R中做到这一点呢?
请查找启发我的Python代码的link

mnemlml8

mnemlml81#

以下R项目可以生成三元图:https://cran.r-project.org/web/packages/Ternary/vignettes/Ternary.html
可以换一种方式问这个问题,比如“是否有一个R库可以帮助我像这个python库那样绘图?”这样的问题会更好地被接受,因为将库从一种语言转换为另一种语言是非常激烈的。

hgb9j2n6

hgb9j2n62#

下面是一个使用Ternary包的示例脚本,遵循其interpolation example

X <- rep(c(.45,.4,.55,.4,.43,.5,.43,.5,.43,.48), each = 3)
Y <- rep(c(.15, .12,.22,.14,.14,.19,.12, .17,.17,.12 ), each = 3)
Z <- rep(c(.15,.22,.12,.12,.19,.14,.14,.17,.12,.17), each = 3)
Gi <- c(353,381,320,312,335,265,394,350,374,320,299,316,300,304,295,360,331,395,351,280,342,299,303,279,374,364,419,306,290,315)

library("Ternary")

# Start a plot, to define the coordinate system
par(mar = rep(0.2, 4))
TernaryPlot(alab = "fat", blab = "lactose", clab = "protein")

abc <- rbind(X, Y, Z)
# Convert measured points to XY
xy <- TernaryToXY(abc)

# Use an inverse distance weighting to interpolate between measured points
Predict <- function(predXY) {
  Distance <- function(a, b) {
    apply(a, 2, function(pt) sqrt(colSums((pt - b) ^ 2)))
  }
  dists <- Distance(xy, predXY)
  id <- 1 / dists
  idw <- id / rowSums(id)
  
  # Return:
  colSums(Gi * t(idw))
}

# Predict at triangle centres
tri <- TriangleCentres(resolution = 12L)
# Adjust the resolution to suit your dataset

# Now we interpolate between our known values to generate a colour for each
# of our tiles
predicted <- Predict(tri[1:2, ])
map <- rbind(x = tri["x", ], y = tri["y", ], z = predicted,
             down = tri["triDown", ])

# Place a semitransparent colour fill over grid lines:
ColourTernary(map)

# Print legend for colour scale
TreeTools::SpectrumLegend(
  x0 = 0.85,
  y0 = 0.6,
  title = "Gi",
  legend = signif(seq(min(Gi), max(Gi), length.out = 4), 3),
  palette = viridisLite::viridis(256L, alpha = 0.6),
  xpd = NA # Do not clip at edge of figure
)

# Calculate contours
PredictABC <- function(a, b, c) Predict(TernaryToXY(rbind(a, b, c)))
TernaryContour(PredictABC, resolution = 36L)

# Mark the location of our data points
TernaryPoints(abc, pch = 3, col = "#cc3333")

相关问题