【问题标题】:How to add a single new contour to a contour plot in Lattice如何在 Lattice 的等高线图中添加一个新的等高线
【发布时间】:2013-08-22 12:30:45
【问题描述】:

我使用 Lattice 包中的轮廓图函数生成了一个填充轮廓图。 我希望能够强调此功能产生的轮廓线之一,例如通过改变 lwd=2,并淡化产生的其他轮廓线,例如通过将剩余的等高线颜色设置为灰色。

例如,我想等价于添加

contourplot(
Male ~ Year * Age, 
data=this.ds, region=F, labels=F, lwd=2, at=c(0, 0.01, 1))

contourplot(
Male ~ Year * Age, 
data=this.ds, 
region=T, col.regions=rev(heat.colors(200)), 
cuts=50, main="", sep="") 

我认识到答案可能涉及函数更新、面板和/或层(如果使用 latticeExtra),但我对 lattice 对象的底层逻辑/结构的理解不足以弄清楚要做什么做。

编辑

感谢您的帮助。我基本上使用了第一个答案,但稍作修改以便能够指出轮廓线上的几个特征:

require(lattice)
require(latticeExtra)

Part1 <- contourplot(Female ~ Year * Age, 
                     data=this.ds,
                     region=T, 
                     col.regions=rev(heat.colors(200)),
                     cuts=50, 
                     main="", 
                     labels=F,
                     col="grey",
                     sep="")

Part2 <- contourplot(Female ~ Year * Age,
                     data=this.ds,
                     region=F,
                     labels=F,
                     lwd=2,
                     at=c(0, 0.01, 1),
                     panel=function(...){
                     panel.contourplot(...)
                      panel.segments(x0=c(1840, 1900, 2000, 1840), 
                                     x1=c(1900, 1900, 2000, 2000), 
                                     y0=c(40.5, 0, 0, 64), 
                                     y1=c(40.5, 40.5, 64, 64), 
                                     lty="dashed")
                     }                    
)


Plot.Final <- Part1+ Part2 

print(Plot.Final)

【问题讨论】:

标签: r graphics contour lattice


【解决方案1】:

加载 latticeExtra 库。那么:

Part1<-contourplot(
    Male ~ Year * Age, 
    data=this.ds, 
    region=T, col.regions=rev(heat.colors(200)), 
    cuts=50, main="", sep="")

Part2<-contourplot(
    Male ~ Year * Age, 
    data=this.ds, region=F, labels=F, lwd=2, at=c(0, 0.01, 1))

Plot.Final<-Part1+Part2

这应该按照您的意愿组合图表。如果需要,您可能需要设置 xlim、ylim 和标签,以便一切正常。

【讨论】:

  • 太好了。谢谢。所以 latticeExtra 让 lattice 在如何指定绘图方面更像 ggplot?
  • 我从来没有用过ggplot-所以希望其他人可以回答这个问题。
  • 谢谢——我不知道这个库。
【解决方案2】:

使用contour 代替contourplot

bar<-matrix(runif(100,nrow=10))
contour(bar)
contour(bar,levels = 0.2, lwd=3, add=TRUE)

编辑:事实证明你甚至不需要这样做。与contourplot 不同,contour 允许您自动指定线宽(或颜色等)的向量,因此contour(bar,lwd=c(2,1,1,1,1,)) 就足够了。

编辑 2:抱歉:这是在 graphics 包中。

【讨论】:

  • 谢谢。不幸的是,我已经有太多使用 lattice 的代码无法从头开始。
猜你喜欢
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
  • 2019-11-17
  • 1970-01-01
相关资源
最近更新 更多