【问题标题】:plot distance and bearing in R在R中绘制距离和方位
【发布时间】:2014-05-23 12:50:18
【问题描述】:

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

我想制作一个两个面板图,其中一个显示起始距离和方位角,另一个显示终止距离和方位角。理想情况下,第二个图将采用颜色编码(或 pch 编码)以显示不同的反应类型。

我的数据是这种格式

      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”列对第二个图中的点进行颜色编码。此外,轴承没有左右指定,所以我可以接受所有点都在中间线的一侧或另一侧,但如果你知道将它们随机放置在中心线的左侧或右侧会很酷。理想情况下,情节应该是这样的。

更新:遵循@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。

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

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

【问题讨论】:

  • 将两个地块合二为一,同一只鸟的起点和终点使用不同的颜色,起点和终点用直线连接会更好吗?
  • 我可能要做的第一件事是计算每次观察的 X 和 Y 距离。 calcXY &lt;- function(tdat) { dist &lt;- as.numeric(tdat[2]) bear &lt;- as.numeric(tdat[3]) bear.rad &lt;- bear*pi/180 Y &lt;- cos( bear.rad) * dist X &lt;- sin( bear.rad) * dist return(c(X,Y)) } XYPos &lt;- apply(dat, 1, calcXY) 之类的东西很抱歉代码都在一行上。应该清楚间距应该在哪里。我不认为这是对您问题的回答是合理的
  • @Adrian:使用分号分隔 cmets 中的代码行。 :)
  • 我定义的 get.coords 函数 here 可能对此有用,例如with(dat, get.coords(bear, dist, 0, 0)).
  • @jinlong - 有超过 4,000 个观察值。我认为这会很快变得非常拥挤。

标签: r plot


【解决方案1】:

如果您想更紧密地重新创建示例图,请尝试以下操作,它使用您的 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')

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

【讨论】:

  • 非常感谢。我对 jinlong 提供的代码进行了更多修改,并且与您在这里所做的非常接近,但肯定会包含您的一些代码。因为我有超过 4000 个点要绘制,所以我选择沿 x 轴放置仪表标记,因为它们会被点覆盖。再次感谢。
【解决方案2】:

不确定我是否了解您的所有要求,但以下是我对“起点”图的解决方案:

#install.packages("plotrix")
library("plotrix")

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)

##Define a function that converts degrees to radians
#NOTE: Authored by Fabio Marroni
#URL: http://fabiomarroni.wordpress.com/2010/12/23/r-function-to-convert-degrees-to-radians/
degrees.to.radians<-function(degrees=45,minutes=30)
{
  if(!is.numeric(minutes)) stop("Please enter a numeric value for minutes!\n")
  if(!is.numeric(degrees)) stop("Please enter a numeric value for degrees!\n")
  decimal<-minutes/60
  c.num<-degrees+decimal
  radians<-c.num*pi/180
  return(radians)
}

#Plot the canvas
plot(0, 0, type = "n", xaxt = "n", yaxt = "n", asp=1,
     xlim = c(0, max(dat$dist)), ylim =  c(0, max(dist)), 
     bty="n", xlab = "", ylab = "", 
     main = "Whatever observations (starting points only)")

#Plot x/y axes
segments(0, 0, max(dat$dist), 0)
segments(0, 0, 0, max(dat$dist))

#Plot axes labels
axis(1, at = seq(0, max(dat$dist), 100), labels = seq(0, max(dat$dist), 100))

#Plot the equal-distance arcs
dist = 100
while(dist < max(dat$dist)){
  draw.arc(0, 0, radius = dist, deg1 = 0, deg2 = 90, n = 100, col = "blue")
  dist <- dist + 100
}

#Plot the 1st point (cause it's always an starting point)
x <- dat[1, ]$dist*sin(degrees.to.radians(dat[1, ]$bear))
y <- dat[1, ]$dist*cos(degrees.to.radians(dat[1, ]$bear))
points(x, y, pch = 21)

for(i in 2:nrow(dat)){
  #Only plot starting points
  if(dat[i, ]$id != dat[i-1, ]$id){
    #Determin the x and y for each point
    x <- dat[i, ]$dist*sin(degrees.to.radians(dat[i, ]$bear))
    y <- dat[i, ]$dist*cos(degrees.to.radians(dat[i, ]$bear))

    #Adding starting points
    points(x, y, pch = 21)
  }
}

如果这是您想要的,您可以将其调整为“终点”情节。您可以在 point() 函数中添加 col 参数,并使用“act”对点进行颜色编码。

【讨论】:

  • 感谢@jinlong 该代码完美地满足了我的需求。感谢您的帮助。
  • @jbaums 我将保留从您之前的帖子中得出的代码,因为它使我实现了目标的 2/3,我认为它可能对其他人有所帮助。
猜你喜欢
  • 2014-08-05
  • 2011-10-12
  • 2018-02-25
  • 2020-10-17
  • 1970-01-01
  • 2018-09-28
  • 2017-08-26
  • 2021-05-02
  • 2011-02-18
相关资源
最近更新 更多