【发布时间】:2019-02-02 09:50:12
【问题描述】:
我的目标是结合使用 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() 图层了。但是,我需要能够添加圆圈并包含数据点。
知道为什么会发生这种情况,或者我做错了什么吗?谢谢!
【问题讨论】:
-
如果我们看不到发生了什么,就很难提供帮助。在寻求帮助时,您应该包含一个简单的reproducible example,其中包含示例输入和所需的输出,可用于测试和验证可能的解决方案。如果您包含
sample的数据,那么我们可以运行代码来查看发生了什么。