Reputation: 103
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:
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
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)
Upvotes: 2