Reputation: 1231
I have the following codes to draw a circle using R. It will add a horizontal line at 0 which is unwanted. I am wondering how to remove that line segment. I tried abline(v=0, col="white")
. But it only lightens the line instead of removing it completely.
x <- seq(-1, 1, by=0.01)
y <- sqrt(1-x^2)
x <- c(x, x)
y <- c(y, -y)
par(mar=c(2, 0, 0, 0), pty="s")
plot(x, y, type="l", xlim=c(-2, 2), ylim=c(-2, 2))
Update:
Thank you for all your answers. I realised that the problem is in the ordering. The following modified the codes will fix the problem. There will not be a zero line anymore.
x <- seq(-1, 1, by=0.01)
y <- sqrt(1-x^2)
x <- c(x, -x)
y <- c(y, -y)
Upvotes: 0
Views: 2086
Reputation: 263362
x <- seq(-1, 1, by=0.01)
y <- sqrt(1-x^2)
x <- c(x, -x)
y <- c(y, -y)
par(mar=c(2, 0, 0, 0), pty="s")
plot(x, y, type="l", xlim=c(-2, 2), ylim=c(-2, 2))
Upvotes: 2
Reputation: 1956
Do you want to a) remove the line or b) plot a circle with no horizontal line in first place?
If a): The line is there because how you join x (x <- c(x,x)). This means x goes from -1 to 1 and then jumps back again. There are two options how to get rid of it:
Add NA in between the two halfcircles:
x <- c(x, NA, x)
y <- c(y, NA, -y)
This works because NA breaks continuous lines when plotting.
Alternatively, re-order the other halfcircle like
o <- order(-x)
x <- c(x, x[o])
y <- c(y, -y[o])
Upvotes: 2
Reputation: 78792
This is an alternate way to draw the circle without said line:
circle <- function(radius=1, num_segments=360, center=c(0, 0)) {
return(list(x=center[1] + radius * cos(seq(0, 2*pi, length.out=num_segments)),
y=center[2] + radius * sin(seq(0, 2*pi, length.out=num_segments))))
}
cir <- circle()
par(mar=c(2, 0, 0, 0), pty="s")
plot(cir$x, cir$y, type="l", xlim=c(-2, 2), ylim=c(-2, 2))
Upvotes: 1