R 基本图形系统中的多个函数,包括
rect()
和 polygon()
,通过其 angle=
和 density=
参数支持交叉影线:
x = c(0, 0.5, 1, 0.5)
y = c(0.5, 1, 0.5, 0)
par(mar=c(0,0,0,0))
plot.new()
polygon(x, y, angle=45, density=10)
如何将类似的剖面线应用于由 grid 图形系统的
grid.polygon()
函数绘制的多边形:
library(grid)
grid.newpage()
grid.polygon(x,y)
我查看了
?grid.polygon
和 ?gpar
的文档,并浏览了 Paul Murrel 的有关 R 图形的书,但到目前为止一无所获。我错过了一些明显的东西吗?如果没有,是否有一些简单的技巧可以实现这一点?
这是一个 gridSVG 的示例,改编自 Paul Murrell 的演示
library(gridSVG)
library(grid)
x = c(0, 0.5, 1, 0.5)
y = c(0.5, 1, 0.5, 0)
grid.newpage()
grid.polygon(x,y, name="goodshape")
pat <- pattern(linesGrob(gp=gpar(col="black",lwd=3)),
width = unit(5, "mm"), height = unit(5, "mm"),
dev.width = 1, dev.height = 1)
# Registering pattern
registerPatternFill("pat", pat)
# Applying pattern fill
grid.patternFill("goodshape", label = "pat")
grid.export("test-pattern.svg")
也允许使用更复杂的 grobs,因为 svg 负责裁剪。
从 R 4.1.0 开始,网格获得了
pattern
功能,允许重复任何其他 grob 作为填充:
library(grid)
# Note that using absolute coordinates like "cm" for
# x and y does not work
cross <- grobTree(
linesGrob( x=unit(c(0,1),"npc"), y=unit(c(0,1),"npc")),
linesGrob( x=unit(c(0,1),"npc"), y=unit(c(1,0),"npc")))
cpat <- pattern(cross,
width = unit(1,"cm"),
height = unit(1,"cm"),
extend = "repeat")
rect <- rectGrob(
width = unit(5, "cm"),
height = unit(5, "cm"),
gp= gpar(fill = cpat))
grid.newpage()
grid.draw(rect)
(这个答案得到了这篇博文的帮助)