'How to add contour line to plot created with image(), MBA function

I am trying to add contour line to a contour plot created with function MBA, see data below and scripts:



#my data 


data<- structure(list(x = c(76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 
76, 77.5005, 77.5005, 77.5005, 77.5005, 77.5005, 77.5005, 77.5005, 
77.5005, 77.5005, 77.5005, 78.7498333333333, 78.7498333333333, 
78.7498333333333, 78.7498333333333, 78.7498333333333, 78.7498333333333, 
78.7498333333333, 78.7498333333333, 78.7498333333333, 78.7498333333333, 
79.6936666666667, 79.6936666666667, 79.6936666666667, 79.6936666666667, 
79.6936666666667, 79.6936666666667, 79.6936666666667, 79.6936666666667, 
79.6936666666667, 79.6936666666667, 80.4965, 80.4965, 80.4965, 
80.4965, 80.4965, 80.4965, 80.4965, 80.4965, 80.4965, 80.4965, 
81.585, 81.585, 81.585, 81.585, 81.585, 81.585, 81.585, 81.585, 
81.585, 81.585, 81.925, 81.925, 81.925, 81.925, 81.925, 81.925, 
81.925, 81.9956666666667, 81.9956666666667, 81.9956666666667, 
81.9956666666667, 81.9956666666667, 81.9956666666667, 81.9956666666667, 
81.9956666666667, 81.9956666666667, 81.9956666666667), y = c(5, 
10, 20, 30, 40, 45, 50, 60, 90, 120, 200, 10, 20, 30, 40, 50, 
60, 90, 120, 150, 175, 10, 20, 30, 40, 50, 60, 70, 90, 120, 200, 
10, 20, 30, 40, 50, 60, 90, 120, 150, 200, 10, 20, 30, 40, 50, 
60, 90, 120, 150, 160, 10, 15, 20, 30, 40, 50, 60, 90, 120, 200, 
10, 15, 30, 60, 90, 120, 200, 10, 20, 30, 40, 50, 60, 90, 120, 
150, 200), z = c(283, 535, 610, 176, 394, 1635, 1575, 303, 31, 
20, 17, 2102, 1875, 2278, 1624, 941, 184, 73, 147, 193, 158, 
851, 800, 885, 855, 802, 1347, 535, 375, 226, 42, 1660, 2539, 
2947, 1047, 555, 535, 356, 248, 299, 199, 3162, 2674, 587, 275, 
161, 108, 60, 61, 25, 19, 1049, 302, 303, 304, 241, 181, 190, 
80, 33, 14, 2714, 2854, 905, 324, 185, 76, 11, 628, 925, 1579, 
1245, 263, 125, 78, 21, 11, 4)), class = "data.frame", row.names = c(NA, 
-78L))



library(MBA)
Contourplot<- function(data){  
  inter <- mba.surf(data, 1500, 1500, extend=TRUE, n=9, m=1, h=6)$xyz.est 
   image(inter, 
        xaxs="r", 
        yaxs="r", 
        ylim = c(105,0), 
        xlim = c(76,84), #just show the lim at 100 m depth max
        col=colorRampPalette(c("blue4", "blue", "deepskyblue", "cyan", "chartreuse2", "gold", "chocolate1", "firebrick2", "brown4"))(100), 
        axes=F)
  points(data$Latitude, data$sampleDepthInMeters, pch=16, col="black", cex=1)   #cex :  number indicating the amount by which plotting text and symbols should be scaled relative to the default. 1=default, 1.5 is 50% larger, 0.5 is 50% smaller, etc.
   axis(2, cex.axis=1.55, labels=TRUE, tick = TRUE)  #font= to change   # if 1.4 it will not shown the 80 m depth label
  axis(1, labels=TRUE, cex.axis=1.6) #cex.axis= to change size labels 
  
contour(x, x, z, col = "black", add = TRUE, method = "edge",
          vfont = c("sans serif", "plain"))

}



Contourplot(data)


the last function used "contour" to add the contour line, does not work as the data must be "These must be in ascending order. " Or on x I have different latitude from 76 to 82, and x the depth and y the value that are increasing/decreasing....

Any help is much appreciated! thanks a lot!

the wishing result is something like withthis black contour lines:

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source