I need to find a way to annotate a reference line at the same angle as the reference line itself.
我需要找到一种方法,以与参考线本身相同的角度注释参考线。
The following statement will produce the reference line and the label above it. However, the slope of the line may change and I need to find a way make sure the annotation is always at the same angle.
以下语句将生成参考线及其上方的标签。但是,线的斜率可能会发生变化,我需要找到一种方法来确保注释始终处于相同的角度。
plot(1:10,1:10)
abline(a=8, b=-1)
text(x=4, y=5, "reference line label", srt=-28)
Is there a simple way to do it in R? Thanks in advance
在R中有一个简单的方法吗?提前致谢
3 个解决方案
#1
13
One way is by setting the plot aspect ratio, using the asp
argument, and then to calculate the angles using the specified asp
:
一种方法是使用asp参数设置绘图宽高比,然后使用指定的asp计算角度:
asp <- 2
plot(1:10,1:10, asp=asp)
abline(a=8, b=-1)
text(x=4, y=5, "reference line label", srt=180/pi*atan(-1*asp))
abline(a=4, b=-2)
text(x=1, y=3, "reference line label", srt=180/pi*atan(-2*asp))
Setting a different asp
:
设置不同的asp:
asp <- 0.8
plot(1:10,1:10, asp=asp)
abline(a=8, b=-1)
text(x=4, y=5, "reference line label", srt=180/pi*atan(-1*asp))
abline(a=4, b=-2)
text(x=1, y=3, "reference line label", srt=180/pi*atan(-2*asp))
#2
8
An addendum to @Andrie's answer: even if you don't manually set par("asp")
, you can recover the current working aspect ratio with the following function:
@Andrie回答的附录:即使你没有手动设置标准杆(“asp”),你也可以使用以下功能恢复当前的工作宽高比:
getCurrentAspect <- function() {
uy <- diff(grconvertY(1:2,"user","inches"))
ux <- diff(grconvertX(1:2,"user","inches"))
uy/ux
}
So you can create your plot: set asp <- getCurrentAspect()
; and proceed with the rest of @Andrie's solution.
所以你可以创建你的情节:设置asp < - getCurrentAspect();然后继续@Andrie的其余解决方案。
For all I know this function exists somewhere in the R ecosystem, but I haven't seen it ...
据我所知,这个功能存在于R生态系统的某个地方,但我还没有看到它......
#3
3
A similar solution with ggplot2
与ggplot2类似的解决方案
data <- data.frame(x = 1:10, y = 1:10)
intercept <- 10
slope <- -1
ggplot(data, aes(x,y)) + geom_point(shape=1) +
geom_abline(intercept = intercept, slope = slope) +
geom_text(x=4, y=5, label="my label", angle=atan(slope)*180/pi)
intercept <- 10
slope <- -2
ggplot(data, aes(x,y)) + geom_point(shape=1) +
geom_abline(intercept = intercept, slope = slope) +
geom_text(x=4, y=5, label="my label", angle=atan(slope)*180/pi)
#1
13
One way is by setting the plot aspect ratio, using the asp
argument, and then to calculate the angles using the specified asp
:
一种方法是使用asp参数设置绘图宽高比,然后使用指定的asp计算角度:
asp <- 2
plot(1:10,1:10, asp=asp)
abline(a=8, b=-1)
text(x=4, y=5, "reference line label", srt=180/pi*atan(-1*asp))
abline(a=4, b=-2)
text(x=1, y=3, "reference line label", srt=180/pi*atan(-2*asp))
Setting a different asp
:
设置不同的asp:
asp <- 0.8
plot(1:10,1:10, asp=asp)
abline(a=8, b=-1)
text(x=4, y=5, "reference line label", srt=180/pi*atan(-1*asp))
abline(a=4, b=-2)
text(x=1, y=3, "reference line label", srt=180/pi*atan(-2*asp))
#2
8
An addendum to @Andrie's answer: even if you don't manually set par("asp")
, you can recover the current working aspect ratio with the following function:
@Andrie回答的附录:即使你没有手动设置标准杆(“asp”),你也可以使用以下功能恢复当前的工作宽高比:
getCurrentAspect <- function() {
uy <- diff(grconvertY(1:2,"user","inches"))
ux <- diff(grconvertX(1:2,"user","inches"))
uy/ux
}
So you can create your plot: set asp <- getCurrentAspect()
; and proceed with the rest of @Andrie's solution.
所以你可以创建你的情节:设置asp < - getCurrentAspect();然后继续@Andrie的其余解决方案。
For all I know this function exists somewhere in the R ecosystem, but I haven't seen it ...
据我所知,这个功能存在于R生态系统的某个地方,但我还没有看到它......
#3
3
A similar solution with ggplot2
与ggplot2类似的解决方案
data <- data.frame(x = 1:10, y = 1:10)
intercept <- 10
slope <- -1
ggplot(data, aes(x,y)) + geom_point(shape=1) +
geom_abline(intercept = intercept, slope = slope) +
geom_text(x=4, y=5, label="my label", angle=atan(slope)*180/pi)
intercept <- 10
slope <- -2
ggplot(data, aes(x,y)) + geom_point(shape=1) +
geom_abline(intercept = intercept, slope = slope) +
geom_text(x=4, y=5, label="my label", angle=atan(slope)*180/pi)