简体   繁体   English

如何使用geom_circle函数绘制圆

[英]How to plot a Circle Using geom_circle function

My goal is to draw the dimensions/lines of an NBA basketball court using a combination of the ggplot2 and ggforce packages. 我的目标是使用ggplot2和ggforce软件包的组合来绘制NBA篮球场的尺寸/线条。 I have used the + geom_segment() layer to successfully draw the line segments (sidelines, free throw lines, etc.), but I am having a hard time using the + geom_circle() and + geom_arc() functions to draw the circles and circle arcs (three point line, half-court circle, etc.) 我已经使用+ geom_segment()层成功绘制了线段(边线,罚球线等),但是我很难使用+ geom_circle()和+ geom_arc()函数绘制圆并圆弧(三点线,半场圆等)

My code is as follows, where object 'sample' is simply a data frame of shots, with x and y coordinates: 我的代码如下,其中对象“样本”只是镜头的数据帧,具有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)

When I add: 当我添加:

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

(which should draw a circle at half-court), no circles appear on the visualization, and the result includes the initial graph (line segment and points for shots), along with a duplicate of all the data points, but offset up and to the right. (应该在半场上画一个圆),在可视化中没有圆出现,并且结果包括初始图形(线段和拍摄点),以及所有数据点的副本,但向上和向后偏移正确的。 To be clear, no errors occur, it's just that the result is not what I'm going for. 要明确的是,没有错误发生,只是结果不是我想要的。

Also, when I remove the geom_point() layer entirely, and start the code like: 另外,当我完全删除geom_point()层时,启动类似以下代码:

ggplot() +
geom_segment(...)

Then I can add the geom_circle() layer successfully. 然后,我可以成功添加geom_circle()层。 However, I need to be able to add the circle and also include the data points. 但是,我需要能够添加圆并还包括数据点。

Any idea why this is happening, or what I'm doing wrong? 知道为什么会这样,或者我做错了什么吗? Thanks! 谢谢!

Not sure why, but adding inherit.aes = FALSE inside the geom_circle call fixes it. 不知道为什么,但是在geom_circle调用中添加inherit.aes = FALSE解决此问题。

# generate sample data
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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM