How do I add regression lines to a scatterplot mat

2019-05-10 02:17发布

问题:

How do I go about adding regression lines to a scatterplot matrix? I have the following script:

NewNEMSIS = read.csv("NewNEMSIS.csv")
library(gclus)
newmatrix = NewNEMSIS[,2:5]
newmatrix.r = abs(cor(newmatrix))
newmatrix.col = dmat.color(newmatrix.r)
area = NewNEMSIS$area
cpairs(newmatrix[which(area=="A"),c('Response','SceneToPatient','TotalScene','TotalCall')], panel.colors=newmatrix.col, gap=.5, main="Scatterplot Matrix of City A Times", ylim=c(0,60), xlim=c(0,60), na.omit=TRUE, )

How can I add splined or sloped regression lines to these scatterplots while keeping them in matrix form? Thanks!

回答1:

cpairs is just a colorized version of the base graphics pairs function, and looking at its code you can see that it does accept the regular set of panel functions described and illustrated in ?pairs. This is a reproducible example (which yours is not):

require(gclus)
png();   judge.cor <- cor(USJudgeRatings)
         judge.color <- dmat.color(judge.cor)
?pairs
#Review the panel functions
?cpairs  
cpairs(USJudgeRatings,panel.colors=judge.color,pch=".",gap=.5, 
        upper.panel=panel.smooth)
dev.off()

You should learn to post an example that illustrates the problem. We have no way of knowing what is in that .csv file so showing that you did that operation is useless in understanding what is going on (over an above knowing that the result is a dataframe). It's fine to use examples from the help pages of functions you are asking about.