如何在 R 中的线图中添加垂直 abline 到图例或在其上写一些东西?

Dhe*_*ary 2 plot r legend ggplot2 line-plot

我使用R中的abline()函数在 x 轴上的某个点添加了一条垂直线(如下图的线图所示),我想为其添加图例或在其上添加一些文本(垂直于 abline)。

那么,可以这样做吗?如果是,那么我该怎么做?

线图示例

G. *_*eck 5

text与指定的参数一起使用。或者使用legend如图所示的函数。

abline是经典图形,因此假设您想要使用该绘图系统,但如果您想要 ggplot2,请参阅在 ggplot2 中添加水平线来绘图和图例,并进行明显的修改以将水平线更改为垂直线。

x <- y <- 1:10; v <- 5  # input
plot(x, y)
abline(v = v, col = "red")
text("Vertical Line", x = v, y = max(y), srt = -90, pos = 4) # near top
text("Vertical Line", x = v, y = min(y), srt = 90, adj = c(0, -0.5)) # near bottom
legend("topleft", legend = "Vertical Line", pch = "|", col = "red")
Run Code Online (Sandbox Code Playgroud)

截屏