Q:

How do I create an ABAB graph in base R?

I am trying to create a graph in base r to show results from a single-case design study. I need to be able to break the line for each section of the study. I know I can use SCVA but I want to draw the graph myself so I can have more control over it. When I add my lines() I cannot get them to align with the points. I'm not sure what I'm missing. I put some sample code below. I also included an image of the graph produced by the code.
x <- c(5, 7, 10, 20, 23, 25, 4, 8, 6, 21, 28, 25)
y <- c("A1", "A1", "A1", "B1", "B1", "B1", "A2", "A2", "A2", "B2", "B2", "B2")

plot(x)
lines(x[y=="A1"])
lines(x[y=="B1"])
lines(x[y=="A2"])
lines(x[y=="B2"])

Plot from the code above.

A:

In base R, you need to specify the x.
plot(x, xaxt="n")
lines(1:3, x[y=="A1"])
lines(4:6, x[y=="B1"])
lines(7:9, x[y=="A2"])
lines(10:12, x[y=="B2"])
axis(side=1, at=c(2,5,8,11), labels=unique(y))

