gpt4 book ai didi

r - 如何使用 ggplot2 填充圆的一部​​分

转载 作者:行者123 更新时间:2023-12-04 17:02:20 24 4
gpt4 key购买 nike

circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
}
dat <- circleFun(c(1,-1),2.3,npoints = 100)
ggplot(dat,aes(x,y)) + geom_path(color="black") + ggtitle("circle")

我正在用 ggplot2 绘图。我只想在第一象限用蓝色填充圆圈。我该怎么办?

谢谢 !

最佳答案

这是你要找的吗?

# Define the circle; add a point at the center if the 'pie slice' if the shape is to be filled
circleFun <- function(center=c(0,0), diameter=1, npoints=100, start=0, end=2, filled=TRUE){
tt <- seq(start*pi, end*pi, length.out=npoints)
df <- data.frame(
x = center[1] + diameter / 2 * cos(tt),
y = center[2] + diameter / 2 * sin(tt)
)
if(filled==TRUE) { #add a point at the center so the whole 'pie slice' is filled
df <- rbind(df, center)
}
return(df)
}

#Define separate data frames for the filled and unfilled circle
quarterCircle <- circleFun(c(1,-1), diameter = 2.3, start=0., end=0.5, filled=TRUE)
fullCircle <- circleFun(c(1, -1), 2.3, start=0, end=2, filled=FALSE)

ggplot() +
geom_polygon(data=quarterCircle, aes(x,y), color="black", fill="black") +
geom_path(data=fullCircle, aes(x, y), color="black") +
coord_equal()

enter image description here

关于r - 如何使用 ggplot2 填充圆的一部​​分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12794596/

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