给指定区域着色? [英] Color a designated area?
问题描述
我想知道是否有一种方法可以只为我使用 grid.circle 创建的圆的一半着色.
I am trying to figure out if there is a way to color only half of a circle that I created using grid.circle.
library(grid)
grid.circle(x=.5, y=.5, r=.25,gp=gpar(lwd=10))
我想让上半部变成蓝色,下半部留白.
I want to make the top half blue, and the bottom half leave white.
感谢您的帮助!
推荐答案
使用 grid.polygon()
和一些基本的三角函数,你可以定义一个函数来做这个
Using grid.polygon()
and some basic trigonometry, you can define a function that'll do this
需要小心谨慎,以便在视口非方形时填充的半圆不会扭曲.为了以匹配 grid.circle()
使用的规则的方式完成此操作,我将原点设置为 "npc"
单位,圆半径设置为 <代码>snpc"代码>个单位.(关于 "npc"
和 "snpc"
的更多含义,请参见 ?unit
和 vignette("grid")
):
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))
这篇关于给指定区域着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!