ggplot - 在点之间添加线条 - 不在点之间

时间:2017-07-19 11:30:01

标签: r ggplot2

我正在尝试为现有情节添加线条。 只需忽略未绘制的数据。 geom_abline让我添加行,但它们会遍历整个图表。 如何裁剪仅绘制的线条,例如介于(0,0)和(10,-10)之间?

library(ggplot2)
  x <- c(2,4,6,4,7,5,3)
y <- c(4,5,6,7,8,6,4)
data <- data.frame(cbind(x,y))
ggplot(data, aes(x= x, y= y)) +
  expand_limits(y = c(25, -60), x = c(20,-5)) +
  geom_abline(intercept = 0, slope = -1) +
  geom_abline(intercept = 0, slope = -.5, linetype="dotted") 

1 个答案:

答案 0 :(得分:2)

也许是这样的:

library(ggplot2)
x <- c(2,4,6,4,7,5,3)
y <- c(4,5,6,7,8,6,4)
data <- data.frame(cbind(x,y))
ggplot(data, aes(x= x, y= y)) +
  expand_limits(y = c(25, -60), x = c(20,-5)) +
  geom_segment(aes(x = 0, xend = 10, y = 0, yend = 0 - 1*10)) +
  geom_segment(aes(x = 0, xend = 10, y = 0, yend = 0 - 0.5*10),
               linetype = 2)