Search code examples
rcolorsplotfillcontour

Filled contour lines from a plot with color in R


I have the following code that will generate a plot with several contour lines. Now I would like to fill those contours or at least one (for example the 25% contour lines) with a specific color. I have try different options but I am not sure how to select the polygon area from a specific contour and filled it with a color.

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)

Here is the code that will generate the plot. Any suggestions will be appreciated.

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

Solution

  • You can add display="filled.contour2" as an argument to plot.kde. For example :

    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")) 
    

    Which gives :

    enter image description here