r - 在R中绘制距离和方位

标签 r plot

我收集了有关船只海鸟干扰的数据。我在船上,带着测距仪双筒望远镜和一个角度板。对于我调查的每只鸟,我都有一个相对于船舶航向的起始距离和方位。我还有鸟 react 的距离和方位(或者在某些情况下没有反应)。

我想制作一个两个面板图,其中一个显示起始距离和轴承位置,另一个显示终止距离和轴承。理想情况下,第二个图将进行颜色编码(或 pch 编码)以显示不同的 react 类型。

我的数据是这种格式

      date_id dist bear act
550 40711_027  200   30   f
551 40711_028  500   45   n
552 40711_028  450   60   n
553 40711_028  400   75   n
554 40711_028  371   80   f
555 40711_029  200    5   f
556 40711_030  200   10   d
557 40711_031  400   30   n
558 40711_031  350   30   d

这是您可以使用的格式的数据
id <- c(1,2,2,2,2,3,4,5,5)
dist <- c(200,500,450,400,371,200,200,400,350)
bear <- c(30,45,60,75,80,5,10,30,30)
act <- c("f","n","n","n","f","f","d","n","d")

dat <- data.frame(id, dist, bear, act)

正如您所看到的,有些 id 重复,有些只有一行。我想在一个情节上绘制第一个 dist 和 bear ,在另一个情节上绘制最后一个 dist 和 bear(每个 id)。对于只有一次观察的鸟类,这些可能是相同的。最好根据“act”列对第二个图中的点进行颜色编码。此外,轴承没有左右指定,所以我可以接受所有点都在中间线的一侧或另一侧,但如果你知道将它们随机放置在中心线的左侧或右侧会很酷。理想情况下,情节看起来像这样。

Dist Bear Plot

更新:遵循@jbaums 的建议,使用他在另一个问题中找到的代码 here .
get.coords <- function(a, d, x0, y0) {
  a <- ifelse(a <= 90, 90 - a, 450 - a)
  data.frame(x = x0 + d * cos(a / 180 * pi), 
             y = y0+ d * sin(a / 180 * pi))
}

rotatedAxis <- function(x0, y0, a, d, symmetrical=FALSE, tickdist, ticklen, ...) {
  if(isTRUE(symmetrical)) {
    axends <- get.coords(c(a, a + 180), d, x0, y0)    
    tick.d <- c(seq(0, d, tickdist), seq(-tickdist, -d, -tickdist))      
  } else {
    axends <- rbind(get.coords(a, d, x0, y0), c(x0, y0))
    tick.d <- seq(0, d, tickdist)
  }
  invisible(lapply(apply(get.coords(a, d=tick.d, x0, y0), 1, function(x) {
    get.coords(a + 90, c(-ticklen, ticklen), x[1], x[2])
  }), function(x) lines(x$x, x$y, ...)))
  lines(axends$x, axends$y, ...)
}

plot.new()
plot.window(xlim=c(-1000,1000),ylim=c(-1000, 1000), asp=1) 
polygon(get.coords(seq(0,180, length.out=1000),1000,0,0),lwd=2)
polygon(get.coords(seq(0,180, length.out=750),750,0,0),lwd=2)
polygon(get.coords(seq(0,180, length.out=500),500,0,0),lwd=2)
polygon(get.coords(seq(0,180, length.out=250),250,0,0),lwd=2)

rotatedAxis(0, 0, a=90, d=1000, tickdist=100, ticklen=1)
rotatedAxis(0, 0, a=45, d=1000, tickdist=100, ticklen=1)
rotatedAxis(0, 0, a=135, d=1000, tickdist=100, ticklen=1)

obs <- with(dat, get.coords(bear, dist, 0, 0))
points(obs)

这给了我这个越来越接近我的目标的绘图图!谢谢@jbaums。

closer

我的问题是我无法弄清楚如何将 90 楔形从 0 绘制到 90(因为这是我收集数据的地方。

当收集到多个观察值时,我仍然需要一些指导来仅选择第一个(以及最后一个)观察值。

最佳答案

如果您想更紧密地重新创建示例图,请尝试以下操作,它使用您的 dat ,以及 get.coords函数原贴here :

# Define function to calculate coordinates given distance and bearing
get.coords <- function(a, d, x0, y0) {
  a <- ifelse(a <= 90, 90 - a, 450 - a)
  data.frame(x = x0 + d * cos(a / 180 * pi), 
             y = y0+ d * sin(a / 180 * pi))
}

# Set up plotting device
plot.new()
par(mar=c(2, 0, 0, 0), oma=rep(0, 4))
plot.window(xlim=c(-1100, 1100), ylim=c(-100, 1100), asp=1)

# Semicircles with radii = 100 through 1000
sapply(seq(100, 1000, 100), function(x) {
  lines(get.coords(seq(270, 450, length.out=1000), x, 0, 0))
})

# Horizontal line
segments(-1000, 0, 1000, 0)

# 45-degree lines
apply(get.coords(c(360-45, 45), 1000, 0, 0), 1, 
      function(x) lines(rbind(x, c(0, 0)), lwd=2))

# Plot white curves over black curves and add text
sapply(seq(100, 1000, 100), function(x) {
  txt <- paste0(x, 'm')
  w <- strwidth(txt, cex=0.9)/2
  a <- atan(w/x)/pi*180
  lines(get.coords(seq(-a, a, length=100), x, 0, 0), 
        lwd=2.5, col='white')
  text(0, x, txt, cex=0.8)
})

# Add points
points(with(dat, get.coords(-bear, dist, 0, 0)), pch=20)

# Add triangle
polygon(c(0, -30, 30), c(-5, -55, -55), col='black')

dist & bearing

请注意,我已将您的点的角度传递给 get.coords-bear ,因为您的示例图表明您正在从正 y 轴逆时针计算方位角。 get.coords函数期望从正 x 轴顺时针计算角度,负角度(如 -bear 出现)将被解释为 360 减去角度。

关于r - 在R中绘制距离和方位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22977453/

相关文章:

R CMD 检查不在 Rtools 目录中查找 gcc

r - 将函数应用于 R 面板数据中的滚动窗口

python - 随时间绘制总和

unix - 来自两个文件的gnuplot绘图数据:在一个x坐标中,在另一个y中

matlab - 使用 HG2-Update 和 'painters' 渲染器将图形导出为 .pdf 格式的矢量图形无法正常工作

r - 在 .filled.contour 中设置自动颜色级别? (克里格法)

R aggregate() by group using some function (e.g., max) 然后投影到其他列

r - lme4混合模型错误

r - 在矩阵上使用带有均值函数的 mapply

r - 用指定颜色的绘图区域外的形状进行注释