我需要一些角度方面的帮助。
根据帮助,使用包fossil
计算轴承和我们获得的函数fossil::earth.bear
“地球上任意两点之间从真北顺时针方向的轴承”。
我有一个用这个函数得到的角度矢量,我需要对它们进行变换,使原点(0º)在x轴(东)上,并且角度逆时针增加。
基本上我需要一种方法可以顺时针旋转90º角度(因此0°将在x轴上“面向东方”),然后计算相反方向的角度(逆时针)。
直观地说,我已经尝试向轴承添加90º(顺时针旋转),然后从360减去它们(计算“相反方向”的角度)。
然而,它不起作用,我高度怀疑每个象限有不同的事情要做,但我无法理解。
在具有虚拟数据和极坐标直方图的测试下面,以证明解决方案不起作用,因为结果向量bearings2
不等于起始向量bearings
# Generate vector with 100 random values between 0 and 360
set.seed(123)
bearing <- runif(100, 0,360)
# generate a histogram with values binned every 5º
breaks = seq(0, 360, by=5)
bearing.cut = cut(bearing, breaks, right=FALSE)
bearing.freq = as.data.frame(table(bearing.cut))
bearing.freq$bearing.cut <- seq(5,360, by = 5)
#plot with ggplot
library(ggplot2)
ggplot(bearing.freq, aes(x = bearing.cut, y = Freq)) +
coord_polar(theta = "x", start = 0 direction = 1) + #start 0 for north, direction 1 for cloclwise
geom_bar(stat = "identity") +
scale_x_continuous(breaks = seq(0, 360, 5))
bearing2 <- 360-(bearing-90)
# repeat the process to generate freq table and plot
breaks = seq(0, 360, by=5)
bearing.cut2 = cut(bearing2, breaks, right=FALSE)
bearing.freq2 = as.data.frame(table(bearing.cut2))
bearing.freq2$bearing.cut <- seq(5,360, by = 5)
#plot with ggplot
library(ggplot2)
ggplot(bearing.freq2, aes(x = bearing.cut2, y = Freq)) +
coord_polar(theta = "x", start = -pi/2, direction = -1) + # now start at E and counterclockwise
geom_bar(stat = "identity") +
scale_x_continuous(breaks = seq(0, 360, 5))
这就是这个产生的情节。显然,如果我的转换是正确的,这两个图应该看起来一样......但他们没有。
**我根据Gregor的建议进行了编辑(并设置了一个种子,因此它是可重复的)。看起来更好但我们失去了0º和90º之间的所有角度。这加强了我最初的想法,即每个象限都有不同的操作,但仍然无法弄明白。不过,谢谢你的提示!
答案 0 :(得分:0)
好吧我想我已经弄明白但不确定为什么会这样。我会把它留在这里,以便将问题标记为已回答。
解决方案是,对于第一象限(角度在0º和90º之间,我们需要计算complementray angle,所以我们需要90-bearing
。对于其余的象限,我们做Gregor建议的(360-(bearing-90)
)。
完整代码下方的可重现示例
library(ggplot2)
set.seed(123)
# 0º at North and clockwise
bearing <- runif(100, 0,360)
#create histogram
breaks = seq(0, 360, by=5) # half-integer sequence
bearing.cut = cut(bearing, breaks, right=FALSE)
bearing.freq = as.data.frame(table(bearing.cut))
bearing.freq$bearing.cut <- seq(5,360, by = 5)
#plot
p1 <- ggplot(bearing.freq, aes(x = bearing.cut, y = Freq)) +
coord_polar(theta = "x", start =0, direction = 1) +
geom_bar(stat = "identity") +
scale_x_continuous(breaks = seq(0, 360, 5))
# transform to 0º at E and counterclockwise
bearing2 <- ifelse(bearing <=90, (90-bearing), (360 - (bearing - 90)))
#create histogram
bearing.cut2 = cut(bearing2, breaks, right=FALSE)
bearing.freq2 = as.data.frame(table(bearing.cut2))
bearing.freq2$bearing.cut <- seq(5,360, by = 5)
# plot
p2 <- ggplot(bearing.freq2, aes(x = bearing.cut, y = Freq)) +
coord_polar(theta = "x", start = -pi/2, direction = -1) +
geom_bar(stat = "identity") +
scale_x_continuous(breaks = seq(0, 360, 5))
require(gridExtra)
grid.arrange(p1, p2, ncol=2)