4

我想弄清楚是否有一种方法可以只为我使用 grid.circle 创建的半个圆圈着色。

    library(grid)
    grid.circle(x=.5, y=.5, r=.25,gp=gpar(lwd=10))

我想让上半部分变成蓝色,下半部分留下白色。

谢谢您的帮助!

4

2 回答 2

5

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))

enter image description here

于 2012-10-22T16:25:15.177 回答
2

这是对 Josh 创造填充和弦的出色作品的修改的初稿:

filledArc <- function(x_origin, y_origin, radius, fillcolor, top=TRUE) {
    theta <- seq(0, pi/2, length = 100)
    if(!top) theta <- theta + pi     ## To fill the bottom instead
    x <- unit(x_origin, "npc") + unit(c(0, cos(theta) * radius, 0), "snpc")
    y <- unit(y_origin, "npc") + unit(c(0, sin(theta) * radius, 0), "snpc")
    grid.polygon(x, y, gp = gpar(fill = fillcolor))
}

filledArc(0.5, 0.5, 0.25, "lightgoldenrod")
filledArc(0.5, 0.5, 0.25, "blue", top=FALSE)
grid.circle(x = .5, y=.5, r=.25,gp=gpar(lwd=10))

在此处输入图像描述

我认为需要做一些进一步的工作才能通过开始和结束 theta(完成)来使其参数化:

filledArc2 <- function(x_origin, y_origin, radius, fillcolor, angle0, angle1) {
    theta.range <- seq(angle0, angle1, length = 100)
    x <- unit(x_origin, "npc") + unit(c(0, cos(theta.range) * radius, 0), "snpc")
    y <- unit(y_origin, "npc") + unit(c(0, sin(theta.range) * radius, 0), "snpc")
    grid.polygon(x, y, gp = gpar(fill = fillcolor))
}

filledArc2(0.5, 0.5, 0.25, "lightgoldenrod", 0,      pi/4)
filledArc2(0.5, 0.5, 0.25, "blue",           pi/4,   pi*(3/2) )
grid.circle(x = .5, y=.5, r=.25,gp=gpar(lwd=10))

在此处输入图像描述

于 2012-10-22T17:59:59.837 回答