[英]ggplot2 polar plot arrows
我可以輕松地使用ggplot2繪制如下圖:
事實上,對於我的數據,它如下所示:
degree value 1 120 0.50 2 30 0.20 3 -120 0.20 4 60 0.50 5 150 0.40 6 -90 0.14 7 -60 0.50 8 0 0.60
第一列是度(從-180到180或從0到360),第二列是對應的值。 所以我想用箭頭繪制從(0,0)到每個數據點的圖形點,但是如下所示:
(來源: matrixlab-examples.com )
我嘗試使用以下代碼:
base <- ggplot(polar, aes(x=degree, y=value))
p <- base + coord_polar()
p <- p + geom_segment(aes(x=0, y=0, xend=degree, yend=value ), arrow=arrow(length=unit(0.3,"cm")) )
print(p)
它產生了一個極坐標圖,但我沒有得到從(0,0)到我的數據點的直箭頭。
我也嘗試使用plotrix包來繪制這個圖。 它的工作原理如下:
3 http://rgm2.lab.nig.ac.jp/RGM_results/plotrix:polar.plot/polar.plot_001_med.png
我無法在此圖表中導入箭頭。
如何使用plotrix包添加箭頭,或者如何使用ggplot2繪制箭頭?
設置數據(來自dput
):
polar <- structure(list(degree = c(120L, 30L, -120L, 60L, 150L, -90L,
-60L, 0L), value = c(0.5, 0.2, 0.2, 0.5, 0.4, 0.14, 0.5, 0.6)), .Names = c("degree",
"value"), class = "data.frame", row.names = c(NA, -8L))
你可以很容易地得到直線 - 你只需要確保你的段從degree
而不是0開始:
library(ggplot2)
base <- ggplot(polar, aes(x=degree, y=value))
p <- base + coord_polar()
p+ geom_segment(aes(y=0, xend=degree, yend=value))
但是,添加箭頭會使得看起來可能存在錯誤(?) - 在計算箭頭的角度時不會考慮坐標變換:
library(grid)
p+ geom_segment(aes(y=0, xend=degree, yend=value) ,
arrow=arrow(length=unit(0.3,"cm")))
您可以(通過繪制自己的箭頭)來解決這個問題:
awid <- 2
p + geom_segment(aes(y=0, xend=degree, yend=value))+
geom_segment(aes(y=value-0.05,yend=value,x=degree-awid/value,xend=degree))+
geom_segment(aes(y=value-0.05,yend=value,x=degree+awid/value,xend=degree))
如果你仔細觀察,你會發現箭頭並不是完全筆直的(如果你awid
更大,效果會更明顯)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.