Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Points to filled.contour in R - at the right place

Tags:

plot

r

I'd like to add a point to an existing filled.contour plot, using the following code:

MyFunction <- function(x,y){
   return(dnorm(sqrt(x^2+y^2)))
}
wrapper <- function(x, y, my.fun, ...) {sapply(seq_along(x), FUN = function(i) my.fun(x[i], y[i], ...))}
meshstep <- 0.5
x<- seq(-20,20,meshstep)
y <-seq(-20,20,meshstep)
z <- outer(x,y,FUN = wrapper, my.fun=MyFunction)
filled.contour(x,y,z, col=rev(heat.colors(n=20, alpha=0.7)), nlevels=15)
points(0,0)

I'm pretty surprised that points(0,0) didn't put a point into the origin of the plot, but roughly located at x=10,y=0. Also, locator() seems to be prompting coordinates with respect to that 'new' coordinate system as well. Why is that?

like image 611
Roland Avatar asked Oct 17 '13 14:10

Roland


People also ask

What is a filled contour plot?

Rather than having discrete contour lines, “filled” contour plots shade the regions between contours according to a colormap. Contour lines themselves are not plotted. Use Contour Plot in combination with this VI to display lines.


1 Answers

You can find a detailed answer here : Plotting a box within filled.contour plots in R?

In short, filled.contour use two different coordinates system, one for the filled contour and one for the legend. To solve your problem, you either have to use another function, or to put your points into the plot.axes argument :

filled.contour(x,y,z, col=rev(heat.colors(n=20, alpha=0.7)), nlevels=15,
               plot.axes={points(0,0)})

enter image description here

like image 98
juba Avatar answered Oct 09 '22 14:10

juba