着色网格贝塞尔对象

时间:2014-10-16 16:04:39

标签: r r-grid

感谢有人能指出我如何着色(填充)这个网格生成的贝塞尔对象的正确方向。文档中的gpar函数似乎没有效果。

require(grid)
x<-c(2,5,5,8,8,8,8,8,8,8,2,2)
y<-c(2,2,8,8,8,8,5,2,2,2,2,2)

grid.newpage()
pushViewport(plotViewport(xscale=c(0, 10), yscale=c(0, 10)))
grid.xaxis(); grid.yaxis()
grid.bezier(x, y, id=rep(1:3,each=4), default.units="native", gp=gpar(fill='red'))

enter image description here

提前致谢。

1 个答案:

答案 0 :(得分:1)

这是使用xspline和基础R图的解决方案:

plot(x, y)
dd <- xspline(x, y, shape = 1,draw=FALSE)
xspline(x, y, shape = 1,draw=TRUE)
xx <- dd$x
yy <- dd$y
polygon(c(xx[1], xx, xx[1]), c(min(yy), yy, min(yy)),    
        col=rgb(1, 0, 0,0.5), border=NA)

enter image description here

您也可以使用grid.xspline以“网格”样式执行相同操作。在这里我使用lattice来避免所有视口头痛:

dat <- data.frame(x=x,y=y)
library(lattice)
xyplot(y~x,data= dat , aspect = "xy",panel =function(x,y)
{

  grid.xspline(x,y,shape= 1,default.units="native",
     gp=gpar(fill='red'),open = FALSE)

})

enter image description here