我可以轻松地使用 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
The first column is the degree (from -180 to 180 or from 0 to 360), the second column is the corresponding values. So I want to draw a graph point from (0,0) to each my data point with arrow but with a circular coordinate as below:
(source: matrixlab-examples.com)
I try to use follow code:
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))
你可以很容易地得到直线——你只需要确保你的线段从度
开始,而不是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
调大,效果会更加明显)。
关于r - ggplot2 极坐标图箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10515703/