簡體   English   中英

如何使用geom_circle函數繪制圓

[英]How to plot a Circle Using geom_circle function

我的目標是使用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解決此問題。

# 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