r-如何注释曲线

时间:2018-12-19 19:39:22

标签: r annotations curve

我想以适当的角度向曲线图的每条线添加一个标签,如下图所示:

enter image description here

这是我谦虚的剧本:

I0 <- log(1)
b <- .1
curve(exp(I0 - b * x), 0, 50, 
      xlab = "No. of Species (R)", ylab = "Rate (I or E)", col = "blue", lwd = 2)
d <- .01
curve(exp(d * x) - 1, 0, 50, add = TRUE, col = "orange", lwd = 2)

I0 <- log(1/2)
curve(exp(I0 - b * x), 0, 50, add = TRUE, lty = 2, col = "green", lwd = 2)
d <- .014
curve(exp(d * x) - 1, 0, 50, add = TRUE, lty = 2, col = "red", lwd = 2)

title(main = "The equilibrium model of island biogeography")

此脚本生成如下所示的图:

enter image description here

我尝试遵循here的建议,但无法弄清楚该如何处理。

有任何提示吗?

1 个答案:

答案 0 :(得分:1)

最棘手的部分是弄清楚如何使用长宽比,这是我从this email中找到的。由于我们使用了不同的绘图设备,因此您将需要更改诸如文本大小以及要使文本位于线条上方的高度等内容。

基本上,我们只是在要标注的每个点上计算曲线的导数,并针对图形窗口的宽高比进行调整。从那里,您可以计算以度为单位的角度。如果必须多次执行此操作,则可能需要考虑为每条曲线及其导数制作一个函数。

upshift = 0.025
I0 <- log(1)
b <- .1
curve(exp(I0 - b * x), 0, 50, xlab = "No. of Species (R)", ylab = "Rate (I or E)", col = "blue", lwd = 2)

# Get aspect ratio
w <- par("pin")[1]/diff(par("usr")[1:2])
h <- par("pin")[2]/diff(par("usr")[3:4])
asp <- w/h

angle = atan(-b * exp(I0) * exp(-b * 10) / asp) * 180 / pi
text(10, exp(I0 - b * 10) + upshift, "Near", srt = angle)

d <- .01
curve(exp(d * x) - 1, 0, 50, add = TRUE, col = "orange", lwd = 2)
angle = atan(d * exp(d * 30) / asp) * 180 / pi
text(30, exp(d * 30)-1 + upshift, "Large", srt = angle)

I0 <- log(1/2)
curve(exp(I0 - b * x), 0, 50, add = TRUE, lty = 2, col = "green", lwd = 2)
angle = atan(-b * exp(I0) * exp(-b * 10) / asp) * 180 / pi
text(5, exp(I0 - b * 5) + upshift, "Far", srt = angle)

d <- .014
curve(exp(d * x) - 1, 0, 50, add = TRUE, lty = 2, col = "red", lwd = 2)
angle = atan(d * exp(d * 30) / asp) * 180 / pi
text(30, exp(d * 30)-1 + upshift, "Small", srt = angle)

title(main = "The equilibrium model of island biogeography")

enter image description here

相关问题