【问题标题】:bivariate inter- and extrapolation to create heatmap that fills up a circle双变量插值和外插以创建填充圆圈的热图
【发布时间】:2017-02-11 16:49:57
【问题描述】:

我在 2D 的单位圆内有几个点。点(红色、绿色)来自两个类别之一。

library(plotrix)

# draw points within circle
n <- 100
d <- data.frame(x= rnorm(n), 
                y= rnorm(n))
d <- d[sqrt(d$x^2 + d$y^2) < 1, ] 
d$value <- ifelse(d$x > 0, 2, 3)
plot(d$x, d$y, xlim=c(-1,1), ylim=c(-1,1), pch=16, asp=1, col=d$value)
draw.circle(0,0,1)

现在我想用平滑的热图覆盖图,指示每个组的区域。

library(akima)
library(scales)

# estimate surface
nxy <- 100
xyo <- seq(-1, 1, len=nxy)
int <- interp(x = d$x, y = d$y, z = d$value, 
                      extrap = T, 
                      xo = xyo, yo = xyo, 
                      nx = nxy, ny=nxy, 
                      linear = T)
colors <- alpha(colorRampPalette(c("red", "yellow", "green"))(40) , .4)
image(xyo, xyo, int$z, add = T, col = colors)

到目前为止一切顺利。我的问题是找到一种方法将热图外推到圆的边缘,这样它就会填满整个圆。有一个论点extrap。在文档中它说:logical flag: should extrapolation be used outside of the convex hull determined by the data points?。我将它设置为TRUE,但是,这似乎不起作用。

任何想法如何估计覆盖整个圆的光滑表面?

【问题讨论】:

  • packageVersion("akima")?
  • akima 版本 0.6-2
  • 好吧,我在细节里找到原因了:No extrapolation can be performed for the linear case.嗯,更好的RTFM :)

标签: r heatmap smoothing


【解决方案1】:

在了解外推需要linear=FALSE 之后,解决方案很简单。首先,我们可以绘制整个矩形的外推值。

library(akima)
library(plotrix)
library(scales)

plot(d$x, d$y, xlim=c(-1,1), ylim=c(-1,1), pch=16, asp=1, col=d$value)
draw.circle(0,0,1)

# estimate surface
nxy <- 100
xyo <- seq(-1, 1, len=nxy)
int <- interp(x = d$x, y = d$y, z = d$value, 
                      extrap = T, 
                      xo = xyo, yo = xyo, 
                      nx = nxy, ny=nxy, 
                      linear = F)
z <- int$z

# extrapolation with tweaking of color breaks
colors <- alpha(colorRampPalette(c("red", "yellow", "green"))(21), .4)
br <- seq(2.3, 2.7, len=20)
image(xyo, xyo, z, add = T, col = colors, breaks=c(0, br, 5))

在最后一步中,必须去除圆外的像素才能得到最终图。

plot(d$x, d$y, xlim=c(-1,1), ylim=c(-1,1), pch=16, asp=1, col=d$value)
draw.circle(0,0,1)

# remove values outside circle
inside <- outer(xyo, xyo, FUN = function(x,y) sqrt(x^2 + y^2) < 1)
z[!inside] <- NA
plot(d$x, d$y, xlim=c(-1,1), ylim=c(-1,1), pch=16, asp=1, col=d$value)
draw.circle(0,0,1)
colors <- alpha(colorRampPalette(c("red", "yellow", "green"))(21), .4)
br <- seq(2.3, 2.7, len=20)
image(xyo, xyo, z, add = T, col = colors, breaks=c(0, br, 5))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2014-10-03
    相关资源
    最近更新 更多