ggplot2极坐标图箭头 [英] ggplot2 polar plot arrows
问题描述
我可以使用ggplot2轻松绘制如下图形:
实际上,对于我的数据来说,它就像下面这样:
度值
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 http://www.matrixlab-examples.com/image-files/polar_plots_1.gif
我尝试使用以下代码:
base < (aes(x = 0,y = 0,xend = 0)),度,yend =价值),arrow =箭头(长度=单位(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 ,数值= 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 =值,x = degree-awid / value,xend = degree))+
geom_segment(aes(y = value-0.05,yend = value,x = degree + awid / value,xend = degree))
如果仔细观察,可以看到箭头并不是非常直的(如果使 awid $ c,效果更加明显$ c>大)。
I can use ggplot2 easily to draw a graph like below:
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. 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 http://www.matrixlab-examples.com/image-files/polar_plots_1.gif
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.
I also try to use plotrix package to draw this graph. It works like below:
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?
Set up data (from 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:
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).
这篇关于ggplot2极坐标图箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!