2012-05-09 178 views
6

我可以用GGPLOT2輕鬆繪製圖形如下圖所示:GGPLOT2極座標圖箭頭

1

其實,對於我的數據,它是象下面這樣:

 
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)的圖表點和每一個數據點的箭頭,但有一個圓形的座標如下:

2 http://www.matrixlab-examples.com/image-files/polar_plots_1.gif

我嘗試使用如下代碼:

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

+0

看看這篇文章:http://stackoverflow.com/questions/42276773/ggplot-connecting-points-in-polar-coordinates-with-a-straight-line – Roland

回答

12

設置數據:

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)) 

enter image description here 但是,添加箭頭使其看起來像可能存在缺陷(?) - 在計算箭頭角度時未考慮座標變換:

library(grid) 
p+ geom_segment(aes(y=0, xend=degree, yend=value) , 
       arrow=arrow(length=unit(0.3,"cm"))) 

enter image description here 你可以(在某種程度上)解決此黑客通過繪製自己的箭頭:

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)) 

enter image description here

如果你仔細觀察,你可以看到,箭頭都沒有非常直(如果你使awid變大,效果會更加明顯)。

+0

非常感謝!我嘗試了幾個可怕的價值觀。看起來awid = 0.5比2好得多。 – boyang

+0

PS在代碼中包含'expand_limits(x = c(-180,180))'是個好主意,以確保您獲得完整的軸範圍... –

+1

PS它確實適用於expand_limits(),但您也可以使用scale_x_continuous(limits = c(-180,180),breaks = c(-90,0,90,180))來獲得更好的視圖show(0, 90,180,-180) – boyang