为指定区域着色? [英] 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屋!