0

I'm trying to add lines to an existing plot. Just ignore the data that is not plotted. geom_abline lets me add lines but they go through the whole graph. How can I crop the lines that they are only plotted e.g. between (0,0) and (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 Answer 1

2

Maybe like this:

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.