gpt4 book ai didi

r - 如何使用 geom_circle 函数绘制圆

转载 作者:行者123 更新时间:2023-12-04 01:28:49 24 4
gpt4 key购买 nike

我的目标是结合使用 ggplot2 和 ggforce 包来绘制 NBA 篮球场的尺寸/线条。我已经使用 + geom_segment() 图层成功绘制了线段(边线、罚球线等),但我很难使用 + geom_circle() 和 + geom_arc() 函数来绘制圆圈和圆弧(三点线、半场圆等)

我的代码如下,其中对象“样本”只是一个镜头数据框,带有 x 和 y 坐标:

ggplot(sample, aes(shot_x, shot_y)) +
geom_point(color = "red", alpha = .2) +
geom_segment(aes(x = 0, xend = 94, y = 0, yend = 0)) +
geom_segment(aes(x = 0, xend = 94, y = 50, yend = 50)) +
geom_segment(aes(x = 0, xend = 0, y = 0, yend = 50)) +
geom_segment(aes(x = 94, xend = 94, y = 50, yend = 0)) +
geom_segment(aes(x = 0, xend = 14, y = 3, yend = 3)) +
geom_segment(aes(x = 80, xend = 94, y = 3, yend = 3)) +
geom_segment(aes(x = 0, xend = 14, y = 47, yend = 47)) +
geom_segment(aes(x = 80, xend = 94, y = 47, yend = 47)) +
geom_segment(aes(x = 47, xend = 47, y = 0, yend = 50)) +
geom_segment(aes(x = 0, xend = 19, y = 19, yend = 19)) +
geom_segment(aes(x = 0, xend = 19, y = 31, yend = 31)) +
geom_segment(aes(x = 75, xend = 94, y = 19, yend = 19)) +
geom_segment(aes(x = 75, xend = 94, y = 31, yend = 31)) +
geom_segment(aes(x = 19, xend = 19, y = 19, yend = 31)) +
geom_segment(aes(x = 75, xend = 75, y = 19, yend = 31)) +
geom_segment(aes(x = 4, xend = 4, y = 22, yend = 28)) +
geom_segment(aes(x = 90, xend = 90, y = 22, yend = 28)) +
coord_fixed(ratio = 1)

当我添加时:

+ geom_circle(aes(x0 = 47, y0 = 25, r = 6))

(应该在半场画一个圆圈),可视化上没有圆圈出现,结果包括初始图形(线段和投篮点),以及所有数据点的副本,但有偏移向上和向右。需要说明的是,没有发生错误,只是结果不是我想要的。

另外,当我完全移除 geom_point() 层,并启动如下代码:

ggplot() +
geom_segment(...)

然后我就可以成功添加 geom_circle() 图层了。但是,我需要能够添加圆圈并包含数据点。

知道为什么会发生这种情况,或者我做错了什么吗?谢谢!

最佳答案

不知道为什么,但是在 geom_circle 调用中添加 inherit.aes = FALSE 可以解决此问题。

library(ggforce) # geom_circle used to be in ggplot2, now is in ggforce
sample = data.frame(shot_x = c(10, 20), shot_y = c(30, 40))
ggplot(sample, aes(shot_x, shot_y)) +
# ... all your segment lines
coord_fixed(ratio = 1) +
geom_circle(aes(x0 = 47, y0 = 25, r = 6), inherit.aes = FALSE)

output

关于r - 如何使用 geom_circle 函数绘制圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52046393/

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