简体   繁体   English

在R中的filled.contour图中绘制一个方框?

[英]Plotting a box within filled.contour plots in R?

I'm trying to plot a box within a filled.contour plot, but unfortunately, when I plot the lines() after the filled.contour plot is created, the figure is shifted to the right because the scale forces the image to the left, but the box stays at the same coordinates. 我正在尝试在filled.contour绘图中绘制一个框,但不幸的是,当我在fill.contour绘图创建后绘制lines()时,图形向右移动,因为刻度强制图像向左,但盒子保持在相同的坐标。 Here's what my code looks like: 这是我的代码的样子:

dev.new(width=6,height=7)
mypredict<-matrix(data=mypredict,nrow=20,ncol=25)
filled.contour(x=seq(from=-1.5,to=1.5,length=20),
y=seq(from=1,to=3.75,length=25),
z=mypredict,
col=hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1)
)
top <- 3.42
bot <- 1.56
lines(c(-1,-1),c(bot,top))
lines(c(1,1),c(bot,top))
lines(c(-1,1),c(top,top))
lines(c(-1,1),c(bot,bot))

Does anyone know how I can plot those lines within the filled.contour function? 有谁知道如何在filled.contour函数中绘制这些线? Otherwise, the lines do not plot correctly onto the main image, since the scale/legend of the graph is placed on the right. 否则,线条无法正确绘制到主图像上,因为图形的比例/图例位于右侧。

Thanks! 谢谢!

The manual page for filled.contour explains the problem (and gives a solution) filled.contour的手册页解释了问题(并给出了解决方案)

This function currently uses the 'layout' function and so is restricted to a full page display. 此功能目前使用“布局”功能,因此仅限于整页显示。 As an alternative consider the 'levelplot' and 'contourplot' functions from the 'lattice' package which work in multipanel displays. 作为替代方案,请考虑“晶格”包中的“levelplot”和“contourplot”函数,这些函数适用于多面板显示。

The output produced by 'filled.contour' is actually a combination of two plots; 'filled.contour'产生的输出实际上是两个图的组合; one is the filled contour and one is the legend. 一个是填充轮廓,一个是图例。 Two separate coordinate systems are set up for these two plots, but they are only used internally - once the function has returned these coordinate systems are lost. 为这两个图设置了两个独立的坐标系,但它们仅在内部使用 - 一旦函数返回,这些坐标系就会丢失。 If you want to annotate the main contour plot, for example to add points, you can specify graphics commands in the 'plot.axes' argument. 如果要注释主轮廓图,例如添加点, 可以在'plot.axes'参数中指定图形命令。 An example is given below. 下面给出一个例子。

So essentially you pass some instructions as the plot.axes parameters to override standard behaviour. 所以基本上你传递一些指令作为plot.axes参数来覆盖标准行为。

In your example: 在你的例子中:

filled.contour(x = seq(from=-1.5,to=1.5,length=20),
      y = seq(from=1,to=3.75,length=25), z = mypredict,
      col = hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1),
      plot.axes = {axis(1); axis(2); rect(left, bottom, right, top);})

Note that you have to recreate the two axes otherwise they will not be drawn. 请注意,您必须重新创建两个轴,否则将不会绘制它们。 Also, no need to use the lines statement, when there is a rect function! 另外,当有rect函数时,不需要使用lines语句! :) :)

Hope this helps 希望这可以帮助

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

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