ggplot2极地图箭头

6

我可以轻松使用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绘制它?

请查看此帖子:https://dev59.com/Pp_ha4cB1Zd3GeqPvDZL - Roland
1个回答

13

设置数据(来自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))

enter image description here

仔细观察,您会发现箭头不是完全直的(如果您将awid放大,效果会更明显)。


非常感谢!我尝试了几个AWID值。看起来,AWID = 0.5比2好得多。 - boyang
在你的代码中加入 expand_limits(x=c(-180,180)) 是一个好主意,这样可以确保你获得完整的轴范围... - Ben Bolker
1
它确实适用于expand_limits(),但是您也可以使用scale_x_continuous(limits=c(-180, 180), breaks=c(-90, 0, 90, 180))来获得更好的视图,显示为(0、90、180、-180)。 - boyang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接