gpt4 book ai didi

r - ggplot2 极坐标图箭头

转载 作者:行者123 更新时间:2023-12-02 04:57:18 26 4
gpt4 key购买 nike

我可以轻松地使用 ggplot2 绘制如下图:

1

事实上,对于我的数据来说,如下所示:

degree  value1   120 0.502   30  0.203   -120    0.204   60  0.505   150 0.406   -90 0.147   -60 0.508   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:

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

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 调大,效果会更加明显)。

关于r - ggplot2 极坐标图箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10515703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com