简体   繁体   English

ggplot2极坐标图箭头

[英]ggplot2 polar plot arrows

I can use ggplot2 easily to draw a graph like below: 我可以轻松地使用ggplot2绘制如下图:

1

In fact, for my data, it is like below: 事实上,对于我的数据,它如下所示:

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. 第一列是度(从-180到180或从0到360),第二列是对应的值。 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: 所以我想用箭头绘制从(0,0)到每个数据点的图形点,但是如下所示:

2
(source: matrixlab-examples.com ) (来源: 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)

It produced a polar plot, but I did not get the straight arrow from (0,0) to my data points. 它产生了一个极坐标图,但我没有得到从(0,0)到我的数据点的直箭头。

I also try to use plotrix package to draw this graph. 我也尝试使用plotrix包来绘制这个图。 It works like below: 它的工作原理如下:

3 http://rgm2.lab.nig.ac.jp/RGM_results/plotrix:polar.plot/polar.plot_001_med.png 3 http://rgm2.lab.nig.ac.jp/RGM_results/plotrix:polar.plot/polar.plot_001_med.png

I can not import arrow in this graph. 我无法在此图表中导入箭头。

How to add arrows using the plotrix package, or how to draw it with ggplot2? 如何使用plotrix包添加箭头,或者如何使用ggplot2绘制箭头?

Set up data (from dput ): 设置数据(来自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))

You can get the straight lines fairly easily -- you just have to make sure your segments start at degree rather than 0: 你可以很容易地得到直线 - 你只需要确保你的段从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))

在此输入图像描述 Adding arrows, however, makes it look like there may be a bug (?) -- the coordinate transform doesn't get taken into account in computing the angle of the arrows: 但是,添加箭头会使得看起来可能存在错误(?) - 在计算箭头的角度时不会考虑坐标变换:

library(grid)
p+ geom_segment(aes(y=0, xend=degree, yend=value) ,
                arrow=arrow(length=unit(0.3,"cm")))

在此输入图像描述 You can (sort of) hack around this by drawing your own arrowheads: 您可以(通过绘制自己的箭头)来解决这个问题:

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

在此输入图像描述

If you look closely, you can see that the arrowheads aren't perfectly straight (the effect is much more obvious if you make awid larger). 如果你仔细观察,你会发现箭头并不是完全笔直的(如果你awid更大,效果会更明显)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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