user377628
user377628

Reputation:

How can I add two lines off the x and y axes to specify a certain point?

I'm plotting two sets of data using the plot function. They intersect at a certain point. I want to indicate this point with a dotted line from the x axis to that point, and a dotted line from the y axis to that point. Is this possible with R?

Upvotes: 0

Views: 47

Answers (2)

Bill the Lizard
Bill the Lizard

Reputation: 405825

Use the abline function with h and v parameters for the horizontal and vertical values, and lty = 3 for a dotted line.

plot(c(-2,3), c(-1,5), type = "n", xlab = "x", ylab = "y", asp = 1)
abline(h = 3, v = 2, lty = 3)

abline example

Upvotes: 2

hrbrmstr
hrbrmstr

Reputation: 78822

set.seed(1492)

dat1 <- data.frame(x=c(sample(1:100, 100, replace=TRUE), 3),
                   y=c(sample(1:200, 100, replace=TRUE), 4))

dat2 <- data.frame(x=c(sample(1:100, 100, replace=TRUE), 3),
                   y=c(sample(1:200, 100, replace=TRUE), 4))


dat1[dat1$x == dat2$x & dat1$y == dat2$y,]

plot(dat1, col="blue")
points(dat2, col="red", add=TRUE)
abline(h=dat1[dat1$x == dat2$x & dat1$y == dat2$y,]$y, lty=3)

enter image description here

Upvotes: 2

Related Questions