【问题标题】:How to overlay a line plot on top of a filled.contour?如何在填充的轮廓上覆盖线图?
【发布时间】:2017-07-11 23:07:09
【问题描述】:

我正在尝试在 fill.contour 顶部放置一个线图,以实现与图 1 here 中描述的效果相似的效果。

require("lattice")
require("latticeExtra")

mat = matrix(c(1:9),nrow=3,byrow=TRUE); time = c(1:3); mid = c(1:3)
mat
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

line = colMeans(mat)

shades = colorRampPalette(c("red", "green"))
filled.contour(mid,time,t(apply(mat,2,rev)),
                 zlim=c(1,9),nlevels=9,col=shades(9)) 
xyplot(line~time,type="l")

最明显的方法是使用图层,但到目前为止它对我没有用。

layer(filled.contour(mid,time,t(apply(mat,2,rev)),
                 zlim=c(1,9),nlevels=9,col=shades(9))) +     
layer(xyplot(line~time,type="l"))

有什么建议吗?

【问题讨论】:

  • 我会在绘制第二个图之前尝试使用par(new=TRUE),但我不使用lattice,所以我不知道它是否适合你。
  • 函数filled.contour 实际上不是lattice 或latticeExtra 包的一部分。它是base R 图形的一部分。它不能与作为 lattice 和 latticeExtra 一部分的 xyplot 或 layer 函数一起使用。

标签: r plot lattice


【解决方案1】:

查看filled.contour 的帮助页面,您可以在“注意”下阅读:如果要注释主等高线图,例如添加点,可以在 plot.axes 中指定图形命令论据。

### your code
require("lattice")
require("latticeExtra")

mat = matrix(c(1:9),nrow=3,byrow=TRUE)
time = c(1:3) 
mid = c(1:3)
line = colMeans(mat)

shades = colorRampPalette(c("red", "green"))

### call to filled.contour with plot.axes{} for some line
filled.contour(mid, time, t(apply(mat, 2, rev)),
           zlim = c(1, 9), nlevels = 9, col = shades(9),
           plot.axes = { axis(1); axis(2); lines(c(1.5,2, 2.5), c(2,2.8, 2.8), col = "white") }
) 

代码产生了这个图:

您还可以使用以下代码绘制对象linetime 之间的关系。备注我必须从 line 中减去 3 以使其适合图表:

filled.contour(mid, time, t(apply(mat, 2, rev)),
               zlim = c(1, 9), nlevels = 9, col = shades(9),
               plot.axes = { axis(1); axis(2); lines(line-3, time, col = "white") }
) 

生成下图:

请告诉我这是否是你想要的。

【讨论】:

    猜你喜欢
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多