How to remove the vertical asymptote using curve function in R
n <- 1e3
from <- -9
to <- 6
tol <- (to - from) / n
curve(
ifelse(abs(x + 2) > tol, (x+3)/(x+2), NA),
from = from,
to = to,
n = n,
xlab = "",
ylab = "",
# xlim = ,
ylim = c(-0.5,2.5),
las = 1,
lwd = 2
)
The reason it does that is because the "continuous" domain of x
hits the most-nonzero finite y
(negative) that R recognizes and then starts again at the most nonzero finite y
(positive) and continues the line through those two points. The answer to this is to stop the curve immediately before that point (x = -2
) and draw a second line just after that point, adding xlim=
to the first call and add=TRUE
to the second. (Most of the options are not needed or even used when add=TRUE
, so I'll omit them here for demonstration of "simple".)
curve(
(x+3)/(x+2),
from = -9,
to = -2.1,
xlab = "",
ylab = "",
xlim = c(-9, 6),
ylim = c(-0.5,2.5),
las = 1,
lwd = 2
)
curve(
(x+3)/(x+2),
from = -1.9,
to = 6,
lwd = 2,
add = TRUE)