从极坐标转换角度

Transform angles from polar coordinates

我需要一些角度方面的帮助。

用包fossil和函数fossil::earth.bear计算轴承,根据帮助,

"the bearing in degrees clockwise from True North between any two points on the globe".

我有一个用这个函数获得的角度矢量,我需要对它们进行变换,使原点 (0º) 在 x 轴(东)上,角度逆时针递增。

基本上我需要一种方法将我的角度顺时针旋转 90º(因此 0º 将在 x 轴上 "facing East"),然后计算相反方向(逆时针)的角度。

凭直觉,我尝试将方位角加上 90º(顺时针旋转),然后从 360 度减去它们(以计算 "opposite direction" 中的角度)。

然而,它不起作用,我高度怀疑每个象限都有不同的事情要做,但我就是想不通。

下面使用虚拟数据和极坐标直方图进行测试,以证明解决方案不起作用,因为结果向量 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º和90º之间的角度,我们需要计算 complementray angle 所以我们需要 90-bearing 。对于其余的象限,我们做什么格雷戈尔建议(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)