我的图纸如下:
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"))
}
在这里,我想用绿色和蓝色填充形状。有没有更简单的方法来实现这一目标。如果我必须使用grid.polygon
,我必须指定很多点来绘制该形状。
答案 0 :(得分: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)