2012-05-09 203 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