Using grid.polygon()
and some basic trigonometry, you can define a function that'll do this
Some picky care needs to be taken so that the filled semicircle is not distorted when the viewport is non-square. To accomplish this in a way that matches the rules used by grid.circle()
, I've set the origin in "npc"
units, and the circle radius in "snpc"
units. (For more on the meanings of "npc"
and "snpc"
, see ?unit
and vignette("grid")
):
library(grid)
filledSemiCircle <- function(x_origin, y_origin, radius, fillcolor, top=TRUE) {
theta <- seq(0, pi, length = 100)
if(!top) theta <- theta + pi ## To fill the bottom instead
x <- unit(x_origin, "npc") + unit(cos(theta) * radius, "snpc")
y <- unit(y_origin, "npc") + unit(sin(theta) * radius, "snpc")
grid.polygon(x, y, gp = gpar(fill = fillcolor))
}
filledSemiCircle(0.5, 0.5, 0.25, "dodgerblue")
filledSemiCircle(0.5, 0.5, 0.25, "gold", top=FALSE)
grid.circle(x = .5, y=.5, r=.25,gp=gpar(lwd=10))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…