Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add points to pairs plot?

Is there any way for me to add some points to a pairs plot?

For example, I can plot the Iris dataset with pairs(iris[1:4]), but I wanted to execute a clustering method (for example, kmeans) over this dataset and plot its resulting centroids on the plot I already had.

It would help too if there's a way to plot the whole data and the centroids together in a single pairs plot in such a way that the centroids can be plotted in a different way. The idea is, I plot pairs(rbind(iris[1:4],centers) (where centers are the three centroids' data) but plotting the three last elements of this matrix in a different way, like changing cex or pch. Is it possible?

like image 578
Chesco Avatar asked Dec 11 '13 13:12

Chesco


People also ask

How do you add points to a plot?

To add new points to an existing plot, use the points() function. The points function has many similar arguments to the plot() function, like x (for the x-coordinates), y (for the y-coordinates), and parameters like col (border color), cex (point size), and pch (symbol type).

How do you do a pairwise plot in R?

To create a Pair Plot in the R Language, we use the pairs() function. The pairs function is provided in R Language by default and it produces a matrix of scatterplots. The pairs() function takes the data frame as an argument and returns a matrix of scatter plots between each pair of variables in the data frame.


1 Answers

You give the solution yourself in the last paragraph of your question. Yes, you can use pch and col in the pairs function.

pairs(rbind(iris[1:4], kmeans(iris[1:4],3)$centers), 
      pch=rep(c(1,2), c(nrow(iris), 3)), 
      col=rep(c(1,2), c(nrow(iris), 3)))
like image 141
shadow Avatar answered Sep 26 '22 01:09

shadow