简体   繁体   English

R:在轮廓图上添加一条线

[英]R: add a line to contour plot

I would like to plot a function f(x, y) using filled.contour() in R and add a line/curve to identify the points where the value of the function is 0. In order to make the example repricable, let's say that the values for my function are those in the volcano dataset and instead of looking for f(x, y) = 0 we want to add a line/curve to identify where the volcano has value 500. How can I do this? 我想在R中使用filled.contour()绘制函数f(x,y)并添加一条线/曲线以标识该函数的值为0的点。为了使示例可重写,我的函数的值是火山数据集中的值,而不是寻找f(x,y)= 0,我们想添加一条线/曲线来标识火山的值500。如何执行此操作? The following code correctly adds a point at point X=500 and Y=500.But how can I add a line so that only points where volcano=500 are joint by a line? 下面的代码正确地在X = 500和Y = 500处添加了一个点,但是如何添加一条线以使只有volcano = 500的点通过一条线连接? I would like to use only base graphics. 我只想使用基本图形。

x <- 10*1:nrow(volcano)
y <- 10*1:ncol(volcano)
filled.contour(x, y, volcano, color = terrain.colors, plot.axes = { axis(1); axis(2); points(500, 500)})

One solution, which is not perfect: 一个不完美的解决方案:

#create a new plot
contour(x,y,volcano)
#use .filled.colour instead of filled.contour
.filled.contour(x, y, volcano,levels=seq(90,200,1),col=terrain.colors(109))
# add a contour plot with specific levels on top of the filled contour
contour(x,y,volcano, level=130,add=T)

you can specify several contour lines by passing a vectoor to the argument level: level = c(130,150) will draw the contours for height 130 and 150 您可以通过将vectoor传递到参数level来指定几条轮廓线:level = c(130,150)将绘制高度为130和150的轮廓

The problem is that filled.contour ads legend bar so that simply calling filled.contour and then contour makes that the two pictures does not match (see note in help(filled.contour)). 问题在于,fill.contour广告的图例栏使简单地调用fill.contour然后轮廓使两张图片不匹配(请参阅help(filled.contour)中的注释)。

However, with this solution, you loose the legend bar. 但是,使用此解决方案,您可以松开图例栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM