gpt4 book ai didi

r - 带多个孔的 geom_polygon

转载 作者:行者123 更新时间:2023-12-04 11:44:50 25 4
gpt4 key购买 nike

我引用了 this question 的答案并有其他问题。

我修改了如下代码:

library(ggplot2)

ids <- letters[1:2]

# IDs and values to use for fill colour
values <- data.frame(
id = ids,
value = c(4,5)
)

# Polygon position
positions <- data.frame(
id = c(rep(ids, each = 10),rep("b",5)),
# shape hole shape hole
x = c(1,4,4,1,1, 2,2,3,3,2, 5,10,10,5,5, 6,6,7,7,6, 8,8,9,9,8),
y = c(1,1,4,4,1, 2,3,3,2,2, 5,5,10,10,5, 6,7,7,6,6, 8,9,9,8,8)
)

# Merge positions and values
datapoly <- merge(values, positions, by=c("id"))

chart <- ggplot(datapoly, aes(x=x, y=y)) +
geom_polygon(aes(group=id, fill=factor(value)),colour="grey") +
scale_fill_discrete("Key")

并给出以下输出:

enter image description here

有一条线穿过两个彩色框,我不太喜欢它,我该如何去除它?谢谢。

最佳答案

我多年前提出的绘制孔的解决方案是确保在每个孔之后,您的 x,y 坐标返回到相同的位置。这会阻止线条四处嗡嗡作响并穿过其他多边形并留下缠绕数算法未填充(或在不应该填充时填充)的开放区域。

所以,如果你有一个数据集,其中前 27 个点是你的外部,然后你有 5、6 和 7 个点的三个洞,构建一个新的数据集是:

newdata = data[c(1:27,28:32,27,33:38,27,39:45,27),] # untested

请注意它是如何在每个洞后跳回到第 27 点的。确保你的洞是顺时针方向的(我认为)。

然后使用新数据绘制但仅填充,不绘制轮廓。如果你想要轮廓,稍后添加它们(使用按环 id 分组的原始数据)

有时您会得到非常非常细的伪像,其中到孔的引出线与引入线的绘制方式并不完全相同,但它们几乎不引人注意。责怪布雷森纳姆。

关于r - 带多个孔的 geom_polygon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12047643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com