How to make a scatterplot matrix in R using subset of data

3.6k views Asked by At

How can I make a scatterplot matrix in R that shows only a subset of my data (in area A, B, C, or D, as opposed to all 4 aggregated)?

I know that the tapply() function allows for a breakdown by subset. For ex:

tapply(data$x, data$y, summary)

...would give me a statistics summary for each subset.

Here is my script for the scatterplot matrix. NEMSIS is my dataset name. I want to make a scatter plot of data from columns 2 through 5.

#Make scatterplot matrix using gclus package.  
install.packages("gclus")
library(gclus)
matrix = NEMSIS[,2:5]
matrix.r = abs(cor(matrix))
matrix.col = dmat.color(matrix.r)
cpairs(matrix, panel.colors=matrix.col, gap=.5, main="Scatterplot Matrix of Times")

Any thoughts on how I can create a scatterplot matrix while incorporating the tapply() function to limit the plotted data to a subset? Thanks!

1

There are 1 answers

0
keegan On BEST ANSWER

You can subset using typical methods for row subsetting; using which() is simple. For example, I want a scatterplot matrix of a few columns of mtcars, but I'm only interested in the rows where cyl is 4.

pairs(mtcars[which(mtcars$cyl==4),c('disp','hp','drat')])