Home > Software design >  Plotting two matrices as scatter plots in R without
Plotting two matrices as scatter plots in R without

Time:10-28

I have two matrices of same size.

x1 = matrix(data = c(1, 3, 4, 5, 5, 3, 3, 1), nrow = 4, ncol= 2, byrow = TRUE)

x2 = matrix(data = c(-1, -4, 3, 7, -2, 2, 4, -1), nrow = 4, ncol= 2, byrow = TRUE)

I want to plot the both on the same scatter plot, however, x should contain all 'x' values from both x1 and x2, and y also should contain all 'y' values from both matrices.

Matplot doesn't seem to do the work, since it only compares the columns.

How can I do this (if possible without using any packages)?

CodePudding user response:

You can use plot

plot(x1, xlim = c(-3, 6), ylim = c(-5, 7), col = "red", xlab = "X", ylab = "Y")
par(new=TRUE)
plot(x2, xlim = c(-3, 6), ylim = c(-5, 7), col = "blue", xlab = "", ylab = "")

CodePudding user response:

We can use pairs

pairs(cbind(c(x1), c(x2)))

Or just remove the dim attributes in 'x1', 'x2' with c to convert to vector and use plot

plot(c(x1), c(x2))
  • Related