从颜色为R的图中填充轮廓线

时间:2013-01-28 09:32:45

标签: r colors plot fill contour

我有以下代码将生成具有多个轮廓线的图。现在我想用特定颜色填充这些轮廓或至少一个(例如25%轮廓线)。我尝试了不同的选项,但我不知道如何从特定轮廓中选择多边形区域并用颜色填充它。

dd1<-read.table(text="dist  depth
            4916.64 8.661827
            4916.64 14.789091
            4916.64 13.555909
            4916.64 12.92816
            4916.64 11.708774
            4916.64 15.28
            4916.64 13.369875
            4916.64 14.039655
            4916.64 13.454545
            4916.64 12.638261
            4916.64 13.251081
            4916.64 14.006341
            4916.64 12.64
            4916.64 15.521818
            4916.64 10.202121
            4916.64 14.816667
            4916.64 15.504
            9674.844    23.93
            11000.151   22.157143
            11414.31    22.72
            11414.31    25.7
            11414.31    19.07
            11414.31    23.085714
            9481.57 17.266667
            11414.31    26.8
            11414.31    19.382222
            5616.09 12.016667
            10658.02    18.873913
            11414.31    25.2
            11414.31    20.9
            11414.31    27.65
            11414.31    22.133333
            11414.31    30.9
            5616.09 23.3
            11172.718   20.391667
            9964.755    23.51
            5616.09 19.43
            5616.09 19.1
            4916.64 18.42
            8515.2  17.683333
            11414.31    22.128571
            11414.31    22.8608
            10391.095   24.955882
            10931.125   25.225
            6444.407    20.228571
            11276.257   23.77619
            10585.993   23.285714
            10641.214   20.653333
            9757.676    24.007143
            11414.31    18.817
            11414.31    23.525
            11414.31    22.873684
            11414.31    26.15
            10486.595   21.9
            11000.151   24.142857
            11414.31    24.3875
            10819.621   20.569231
            10360.088   29.345455
            9708.951    21.488235
            11414.31    30.775
            11414.31    25.5
            11414.31    18.477917
            10327.144   26.8625
            11414.31    26.12963
            11414.31    29.28125
            11414.31    23.166667
            10689.532   21.8625
            11414.31    28.328571
            11414.31    22.563158
            11414.31    25.490909
            11414.31    26.0625
            11414.31    34.5
            11414.31    17.375294",header=T)

以下是生成图表的代码。任何建议将不胜感激。

library(ks)
## auto bandwidth selection
H.pi2<-Hpi(dd1,binned=TRUE)*1
ddhat<-kde(dd1,H=H.pi2)

# Kernel contour plot
plot(ddhat,cont=c(95),drawpoints=TRUE,col="black",xlab="Distance (m)",lwd=2.5, 
        ylab="Depth (m)",ptcol="grey15",cex=0.7,
        xlim=c(min(dd1[,1]-dd1[,1]*0.4),max(dd1[,1]+dd1[,1]*0.4)),ylim=c(45,-1)) 

plot(ddhat,cont=c(25),add=TRUE,col="red",lwd=2.4)
plot(ddhat,cont=c(50),add=TRUE,col="seagreen2",lwd=2.4)
plot(ddhat,cont=c(75),add=TRUE,col="royalblue",lty=5,lwd=2.5)  

# End of the script

1 个答案:

答案 0 :(得分:2)

您可以将display="filled.contour2"作为参数添加到plot.kde。例如:

plot(ddhat,cont=c(75),add=FALSE,lty=5,lwd=2.5, display="filled.contour2", col=c(NA,"blue"))
plot(ddhat,cont=c(95),drawpoints=TRUE,col="black",xlab="Distance (m)",lwd=2.5, 
      ylab="Depth (m)",ptcol="grey15",cex=0.7,
      xlim=c(min(dd1[,1]-dd1[,1]*0.4),max(dd1[,1]+dd1[,1]*0.4)),ylim=c(45,-1),add=TRUE) 
plot(ddhat,cont=c(50),add=TRUE,lwd=2.4, display="filled.contour2", col=c(NA,"green"))
plot(ddhat,cont=c(25),add=TRUE,lwd=2.4, display="filled.contour2",col=c(NA,"red")) 

给出了:

enter image description here