2017-02-10 5 views
1

2Dの単位円内にいくつかの点があります。ポイント(赤、緑)は2つのクラスのいずれかから来ます。サークルを塗りつぶすヒートマップを作成するための内外の二変量

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に設定しましたが、これはうまくいかないようです。

サークル全体をカバーする滑らかな表面を見積もる方法はありますか?

+0

'packageVersion( "アキマ")'? –

+0

'akima'バージョン0.6-2 –

+0

さて、私は細部の理由を知りました。「線形の場合には外挿はできません。」 –

答えて

0

外挿には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)) 

enter image description here

、円の外側のピクセルは、最終的なプロットを得るために除去しなければなりません。

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

関連する問題