LaTeXFan
LaTeXFan

Reputation: 1231

how to remove unwanted lines in R

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))

enter image description here

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

Answers (4)

IRTFM
IRTFM

Reputation: 263362

enter image description hereWhy not:

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

S. Elzwawi
S. Elzwawi

Reputation: 541

This should solve the problem:

abline(h=0, lwd=1, col="white")

Upvotes: 1

Ott Toomet
Ott Toomet

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:

  1. 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.

  2. Alternatively, re-order the other halfcircle like

    o <- order(-x)
    x <- c(x, x[o])
    y <- c(y, -y[o])
    

Upvotes: 2

hrbrmstr
hrbrmstr

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))

enter image description here

Upvotes: 1

Related Questions