有没有简单的方法来设置 ggplot 中垂直或水平线的限制?

Cob*_*bin 6 r ggplot2

我想绘制部分geom_vlineor geom_hline,代码如下:

df <- data.frame(x=1:10,y=1:10)
plt <- ggplot(data=df)+
  geom_point(aes(x=x,y=y))+
  geom_vline(aes(xintercept=x[2]))+
  geom_hline(aes(yintercept=y[2]))
Run Code Online (Sandbox Code Playgroud)

我希望将左侧和下部的部分线显示为交叉点。但没有诸如xlimylim之类 的争论geom_vline

在此输入图像描述

www*_*www 11

您可以使用geom_line

library(ggplot2)

df <- data.frame(x=1:10,y=1:10)
plt <- ggplot(data=df)+
  geom_point(aes(x = x, y = y))+
  geom_line(data = data.frame(x = c(2, Inf), y = c(2, 2)), aes(x = x , y = y)) +
  geom_line(data = data.frame(x = c(2, 2), y = c(2, Inf)), aes(x = x, y = y)) 
Run Code Online (Sandbox Code Playgroud)

或者geom_segment。他们导致了相同的情节。

plt <- ggplot(data=df)+
  geom_point(aes(x = x, y = y))+
  geom_segment(aes(x = 2 , y = 2, xend = Inf, yend = 2)) +
  geom_segment(aes(x = 2 , y = 2, xend = 2, yend = Inf))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述