-->

GGPLOT2极坐标图箭头(ggplot2 polar plot arrows)

2019-06-24 06:59发布

我可以用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),第2栏是相应的值。 所以我想借鉴(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画呢?

Answer 1:

(从设置数据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大)。



文章来源: ggplot2 polar plot arrows