Q:

Looping points() with segments() R

I connected points with segments in a Cartesian coordinate system. I stuck by looping the points to the segments, so that I do not have to enter the coordinates for points and segments every time if I want to change them. A for loop would probably help here.
x <- -10:10
y <- -10:10

plot(x, y, type = "n", xlab = NA, ylab = NA, axes = FALSE);

axis(1, pos = 0, at = seq(-10, 10, by = 1), las=1, cex.axis=0.5)
axis(2, pos = 0, at = seq(-10, 10, by = 1), las=2, cex.axis=0.5)

points(2,1, col = "blue", pch = 20)
points(5,7, col = "blue", pch = 20)
points(-3,8, col = "green", pch = 20)
points(8,3, col = "green", pch = 20)

segments(2, 1, 5 , 7)
segments(-3, 8, 8, 3)

A:

You are repeating the code that plots the points and draws the segments connecting them. In these cases it's better to write a function and call it whenever needed.  
Note function argument pcol, standing for point color and scol for the segment color.
connectPoints <- function(x, y, pcol, pch = 20, scol = "black"){
  points(x[1], x[2], col = pcol, pch = pch)
  points(y[1], y[2], col = pcol, pch = pch)
  segments(x[1], x[2], y[1], y[2], col = scol)
}

plot(x, y, type = "n", xlab = NA, ylab = NA, axes = FALSE);

axis(1, pos = 0, at = seq(-10, 10, by = 1), las=1, cex.axis=0.5)
axis(2, pos = 0, at = seq(-10, 10, by = 1), las=2, cex.axis=0.5)

x1 <- c(2, 1)
y1 <- c(5, 7)
x2 <- c(-3, 8)
y2 <- c(8, 3)

connectPoints(x1, y1, pcol = "blue")
connectPoints(x2, y2, pcol = "green")

