r - 如何注释曲线
r - How to annotate curves
我想以适当的角度为曲线图的每条线添加标签,如下图所示:
这是我简陋的脚本:
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")
此脚本生成如下图所示:
我尝试遵循 here 的建议,但无法弄清楚如何使用我的情节。
有什么提示吗?
最棘手的部分是弄清楚如何使用纵横比,这是我从 this email 中找到的。由于我们有不同的绘图设备,您需要更改文本大小以及您希望文本超出线条的高度等内容。
基本上,我们只是在要进行注释的每个点处计算曲线的导数,并针对图形的纵横比进行调整window。从那里,您可以计算角度(以度为单位)。如果您必须多次执行此操作,您可能需要考虑为每条曲线及其导数创建一个函数。
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")
我想以适当的角度为曲线图的每条线添加标签,如下图所示:
这是我简陋的脚本:
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")
此脚本生成如下图所示:
我尝试遵循 here 的建议,但无法弄清楚如何使用我的情节。
有什么提示吗?
最棘手的部分是弄清楚如何使用纵横比,这是我从 this email 中找到的。由于我们有不同的绘图设备,您需要更改文本大小以及您希望文本超出线条的高度等内容。
基本上,我们只是在要进行注释的每个点处计算曲线的导数,并针对图形的纵横比进行调整window。从那里,您可以计算角度(以度为单位)。如果您必须多次执行此操作,您可能需要考虑为每条曲线及其导数创建一个函数。
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")