繁体   English   中英

ggplot2极坐标图箭头

[英]ggplot2 polar plot arrows

我可以轻松地使用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
(来源: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM