Julia Kim
Julia Kim

Reputation: 27

Plotting scatter plot in R

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

Upvotes: 0

Views: 164

Answers (1)

vikjam
vikjam

Reputation: 550

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

Upvotes: 1

Related Questions