How do I add regression lines to a scatterplot matrix?

跟風遠走 提交于 2019-12-10 06:00:48

问题


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.



来源:https://stackoverflow.com/questions/27194627/how-do-i-add-regression-lines-to-a-scatterplot-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!