在 R 中向 fill.contour 添加点 - 在正确的位置 [英] Adding Points to filled.contour in R - at the right place
问题描述
我想使用以下代码向现有的 filled.contour
图中添加一个点:
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)
我很惊讶 points(0,0)
没有在图的原点放置一个点,而是大致位于 x=10,y=0.此外, locator()
似乎也在提示相对于新"坐标系的坐标.这是为什么?
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?
推荐答案
最好的选择是使用 @juba 提到的 plot.axes
参数.但是,如果你真的需要在绘图完成后添加一些东西,那么你可以使用 locator
单击绘图中的 2 个点,在那里你知道坐标系中你想要使用的点的值(对角),然后使用TeachingDemos 包中的updateusr
函数将当前坐标系修改为您要使用的坐标系.然后,您可以使用新坐标系添加到绘图中(您可能需要设置 par(xpd=NA)
).
The best option is to use the plot.axes
argument as mentioned by @juba. But, if you really need to add something after the plot has finished then you can use locator
to click on 2 points in the plot where you know the values of the points in the coordinate system you want to use (opposite corners), then use the updateusr
function from the TeachingDemos package to modify the current coordinate system to the one that you want to use. You can then add to the plot using the new coordinate system (you may need to set par(xpd=NA)
).
这篇关于在 R 中向 fill.contour 添加点 - 在正确的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!