# load ggplot2 library
library(ggplot2)
# create example data
x <- rnorm(50)
y <- x + rnorm(50)
# create ggplot object with equal axis lengths
p <- ggplot(data = data.frame(x, y), aes(x = x, y = y)) +
geom_point() + geom_abline()
p <- p + scale_y_continuous(breaks = seq(-3,3, by=1),limits = c(-3,3))
p <- p + scale_x_continuous(breaks = seq(-3,3, by=1),limits = c(-3,3))
# add vertical and horizontal lines
p <- p + geom_vline(xintercept = -1, color = "blue") +
geom_hline(yintercept = -1, color = "blue")
我希望线在任何一个轴上都不高于-1,但似乎不知道如何做到这一点。任何帮助都表示感谢
1条答案
按热度按时间h6my8fg21#
您可以使用两个
geom_segment
来指定正确的x和y坐标,如下所示:创建于2023-04-08使用reprex v2.0.2