Douglas Fir
Douglas Fir

Reputation: 103

How to remove line between discontinuities in R

I want to plot the following piecewise function in R: f(x)=x if x<=1/2, f(x)=x-1 if x>1/2. However, I haven't been able to figure out how to remove the line joining the two points between the discontinuity at x=1/2. My code is below:

x<-seq(0,1,1/255)
fx<-ifelse(x<=1/2,x,x-1)
plot(x,fx,ylim=c(-1,1),type='l')

And here is the output:

enter image description here

Is there a way to remove only the line joining those two points, but keep everything else? Any help would be greatly appreciated. Thank you!

Upvotes: 0

Views: 151

Answers (1)

Benjamin Christoffersen
Benjamin Christoffersen

Reputation: 4841

Here is a solution with curve

plot(1, ylim=c(-1,1), xlim = c(0, 1), type = "n")
curve(x + 0, from = 0, to = 1/2, add = TRUE)
curve(x - 1, from = 1/2, to = 1, add = TRUE)

enter image description here

Upvotes: 2

Related Questions