0

I have some problem printing out two plots in one graph although I have used the option 'add=TRUE' Plz check out is there any point to fix out.

plot(X1[Y==0], type="p",xlim=c(0,8),ylim=c(0,40),col=4,pch=1,ylab="X1")
plot(X1[Y==1], add=TRUE, type="p",xlim=c(0,8),ylim=c(0,40),col=2, pch=2, ylab="X1")
neilfws
  • 26,280
  • 5
  • 44
  • 53
Julia Kim
  • 27
  • 4
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Normally you would just use `points()` to add the second set of points. What what exactly is the problem you are seening? – MrFlick Dec 15 '20 at 04:32
  • @MrFlick If I run this, error comes out with the comment "add" is not a design related component. I will try for the ```points()``` – Julia Kim Dec 15 '20 at 04:40
  • @MrFlick Thanks! It worked!! :) – Julia Kim Dec 15 '20 at 05:06

1 Answers1

1

Let's create some sample data to illustrate your situation.

X1 <- 1:8
print(X1)
# [1]  1  2  3  4  5  6  7  8

Y <- rep(c(0, 1), times = 4)
print(Y)
# [1] 0 1 0 1 0 1 0 1

If you want to reuse the same plot window to layer your graphs, avoid using plot the second time.

See the answer to a similar question: Plot two graphs in same plot in R

Applied to your example, this code should overlay the graphs.

plot(X1[Y==0], type="p",xlim=c(0,8),ylim=c(0,40),col=4,pch=1,ylab="X1")
points(X1[Y==1], type="p",xlim=c(0,8),ylim=c(0,40),col=2, pch=2, ylab="X1")
vikjam
  • 455
  • 3
  • 8