双变量间插值和外推法创建填充圆形的热图

时间:2017-02-10 09:20:18

标签: r heatmap smoothing

我在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)

enter image description here

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

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)

enter image description here

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

有关如何估算覆盖整个圆圈的光滑表面的任何想法吗?

1 个答案:

答案 0 :(得分:0)

在了解推断需要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))

enter image description here

在最后一步中,必须移除圆外的像素以获得最终的图。

import Foundation

class SharedClass {
  static let sharedInstance = SharedClass()
  var phoneNumber = ""
}

enter image description here