使用网格填充在R中绘制的贝塞尔曲线

问题描述 投票:4回答:1

我的图纸如下:

library(grid)

grid.newpage()

grid.rect(x = 0.1, y = 0.4, width = unit(0.18, "npc"), 
          height = unit(0.6, "npc"))
grid.rect(x = 0.65, y = 0.4, width = unit(0.6, "npc"), 
          height = unit(0.6, "npc"))

for (i in c(0.1, 0.17)) {
  x <- c(0.08, 0.05, i, 0.08)
  y <- c(0.15, 0.25, 0.25, 0.5)
  grid.bezier(x, y, gp = gpar(col = "green3"))
}

for (i in c(0.38, 0.8)) {
  x <- c(0.4, i, i, 0.9)
  y <- c(0.4, i, 0.15, 0.15)
  grid.bezier(x, y, gp = gpar(col = "blue3"))
}

这给了我下面的图,Figure-1

在这里,我想填补绿色和蓝色的形状。有没有更简单的方法来实现这一目标。如果我必须使用grid.polygon,我必须指定许多点来绘制该形状。

r plot bezier r-grid
1个回答
0
投票

你考虑过使用grid.xspline吗?它可以采取封闭的形状(open = FALSE),因此是fill颜色。但是,形状会有所不同,如下所示。

library(grid)
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 1)))

## Beziers
pushViewport(viewport(layout.pos.row = 1))
grid.text("Bezier", x = 0.05, y = 0.8, just = "left", gp=gpar(cex = 2))

grid.rect(x = 0.1, y = 0.4, width = unit(0.18, "npc"), 
          height = unit(0.6, "npc"))
grid.rect(x = 0.65, y = 0.4, width = unit(0.6, "npc"), 
          height = unit(0.6, "npc"))

x1 <- c(0.08, 0.05, 0.1, 0.08, 0.08, 0.17, 0.05, 0.08)
y1 <- c(0.15, 0.25, 0.25, 0.5, 0.5, 0.25, 0.25, 0.15)
id <- rep(1:2, each = 4)
grid.bezier(x1, y1, id = id, gp = gpar(col = "green3"))

x2 <- c(0.4, .38, .38, 0.9, .9, .8, .8, .4)
y2 <- c(0.4, .38, 0.15, 0.15, .15, .15, .8, .4)
grid.bezier(x2, y2, id = id, gp = gpar(col = "blue4"))

upViewport(1)

## X-splines
pushViewport(viewport(layout.pos.row = 2))
grid.text("X-spline", x = .05, y = 0.8, just = "left", gp=gpar(cex = 2))

grid.rect(x = 0.1, y = 0.4, width = unit(0.18, "npc"), 
          height = unit(0.6, "npc"))
grid.rect(x = 0.65, y = 0.4, width = unit(0.6, "npc"), 
          height = unit(0.6, "npc"))

s <- c(0, 1, 1, 0, 0, 1, 1, 0)
grid.xspline(x1, y1, shape = s, open = FALSE, gp = gpar(col = "green3", fill = "green3"))
grid.xspline(x2, y2, shape = s, open = FALSE, gp = gpar(col = "blue4", fill = "blue4"))

upViewport(0)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.