2017-08-13 139 views
2

我試圖用R中的ks包繪製一個透明填充的二維核密度圖 這是我的代碼。二維核密度圖和透明輪廓填充R

library(ks) 
B<-matrix(c(3.02,2.71,8.05,1.36,1.18,2.64,1.48,5.51,3.25,2.66,0.15,2.25,3.09,2.49,3.37,4.56,0.15,0.57,2.6,0.24,0.27,0.1,0.34,0.24,0.24,0.24,0.23,0.34,0.36,0.1,12,0.88,0.15,0.2,0.1,11.19,0.1,0.1,0.26,22.3),nrow=20,ncol=2) 
f<-kde(B) 
png(filename = "KDE_test.png", units="in", width=5, height=5, res=100) 
plot(f,display="filled.contour2",xlim=c(-1,5),ylim=c(-3.5,5),lwd=0,lwd.fc=0.1,drawlabels=FALSE,col=c("transparent",rgb(0,0,255, maxColorValue=255, alpha=50),rgb(0,0,255, maxColorValue=255, alpha=100),rgb(0,0,255, maxColorValue=255, alpha=150))) 
dev.off() 

但是,我最終得到了一個有鋸齒邊緣的KDE圖。抱歉,我無法上傳我的照片,但請運行此代碼,您將看到我正在談論的圖像。

有誰知道如何解決這個問題,或者我做錯了什麼?

非常感謝。

enter image description here

回答

1

您可以使用kdegridsize選項。

library(ks) 
B <- matrix(c(3.02,2.71,8.05,1.36,1.18,2.64,1.48,5.51,3.25,2.66,0.15,2.25,3.09, 
    2.49,3.37,4.56,0.15,0.57,2.6,0.24,0.27,0.1,0.34,0.24,0.24,0.24,0.23, 
    0.34,0.36,0.1,12,0.88,0.15,0.2,0.1,11.19,0.1,0.1,0.26,22.3), 
    nrow=20, ncol=2) 

f<-kde(B, gridsize=1024) 

plot(f,display="filled.contour2", xlim=c(-1,5), ylim=c(-3.5,5), 
    lwd=0, lwd.fc=0.1, drawlabels=FALSE, 
    col=c("transparent", 
     rgb(0,0,255, maxColorValue=255, alpha=50), 
     rgb(0,0,255, maxColorValue=255, alpha=100), 
     rgb(0,0,255, maxColorValue=255, alpha=150))) 

enter image description here

+0

感謝。這很容易。 –