如何在fill.contour上叠加线图?

时间:2017-07-11 23:07:09

标签: r plot lattice

我试图在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"))

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

查看filled.contour的帮助页面,您可以阅读“注意”:如果要注释主要轮廓图,例如添加点,可以在绘图中指定图形命令.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") }
) 

代码产生了这个图:

enter image description here

您还可以使用以下代码绘制对象linetime之间的关系。请注意,我必须从3中减去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(line-3, time, col = "white") }
) 

产生下图: enter image description here

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