geotheory
geotheory

Reputation: 23680

Colouring grid bezier objects

Grateful if someone could point me in the right direction for how to colour (fill) this grid-produced bezier object. The gpar function in the documentation seems to have no effect.

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

Thanks in advance.

Upvotes: 2

Views: 170

Answers (1)

agstudy
agstudy

Reputation: 121608

Here a solution using xspline and base R plot :

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

You can also do the same in "grid" style , using grid.xspline . here I am using lattice to avoid all viewport headache :

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

Upvotes: 1

Related Questions