car::scatterplotmatrix散点图矩阵 参数详解

转载自:https://blog.csdn.net/qq_20828983/article/details/95659791

在R语言实战第二版书,第八章回归分析时,用到了scatterplotmatrix 函数绘制散点图矩阵,发现已经不是当前最新的car包了,函数参数都错误了。在网上百度发现基本没有关于此函数的详细介绍,只有自己动手,查看help了。趁热打铁,写个说明。

基于的car版本为:3.0-3

目录

一睹为快

1、数据添加

2、smooth 参数

3、id 控制点标识

4、groups分组

5、diagonal 对角线参数设置

6、legend 图例

附scatterplotMatrix函数源码:


先看函数的用法:

 
  1. scatterplotMatrix(formula, data=NULL, subset, ...)

  2.  
  3. scatterplotMatrix(x, smooth = TRUE,

  4.  
  5. id = FALSE, legend = TRUE, regLine = TRUE,

  6.  
  7. ellipse = FALSE, var.labels = colnames(x), diagonal = TRUE,

  8.  
  9. plot.points = TRUE, groups = NULL, by.groups = TRUE,

  10.  
  11. use = c("complete.obs", "pairwise.complete.obs"), col =

  12.  
  13. carPalette()[-1], pch = 1:n.groups, cex = par("cex"),

  14.  
  15. cex.axis = par("cex.axis"), cex.labels = NULL,

  16.  
  17. cex.main = par("cex.main"), row1attop = TRUE, ...)

  18.  
  19. spm(x, ...)

下面对每个参数进行解释

 
  1. scatterplotMatrix(x,

  2. #smooth = list(spread = T, col.smooth = "black", col.spread = "black", lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2)

  3. smooth = TRUE, #平滑曲线(多次拟合)False不绘制拟合曲线,True绘制,不分组则按全体,分组则按分组拟合。

  4. #id 控制点标识;如果为false(默认),则不标识任何点;可以是 ShowLabels 函数的命名参数列表;

  5. #true相当于list(method=“mahal”,n=2,cex=1,location=“lr”),

  6. #它用最大的maha标识2个点(在每个组中,如果为by.groups=true)与数据中心的距离;

  7. #不允许使用列表(method=“identify”)进行交互点标识。

  8. id = FALSE,

  9. #legend 图例 默认legend = TRUE 为 legend = list(coords= "topright"),

  10. #可以是一个列表,其中命名的elementcoords以图例函数可接受的任何形式指定图例的位置;

  11. #c("bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right", "center")

  12. legend = TRUE, #根据分组绘制图例,并控制图例的位置;如果为false,则不绘制图例。

  13. #regline 控制向每个绘图或每组点添加一条拟合回归线(如果by.groups=true)

  14. #如果regline=false,则不绘制任何线条。

  15. #此参数也可以是具有命名列表的列表,默认regline=true相当于regline=list(method=lm,lty=1,lwd=2,col=col[1]),

  16. #指定计算行的函数的名称,行类型1(solid)的相对行宽为2,颜色等于a中的第一个值。

  17. #设置method=MASS::rlm将使用稳健回归拟合。

  18. regLine = TRUE, #回归线,TRUE绘制,flase不绘制

  19. #ellipse 控制绘制数据集中椭圆。 如果为“false”(默认),则不绘制椭圆

  20. #可以是给定水平命名值的列表,或者一个向量的一个或多个二元正态概率等值线水平,在其中绘制椭圆;

  21. #鲁棒性,确定是否使用质量包中的cov.trob函数来计算数据椭圆的中心和协方差矩阵的逻辑值。

  22. #以及fill and fill.alpha,它控制椭圆是否被填充以及填充的透明度。

  23. #true相当于list(levels=c(.5,.95),robust=true,fill=true,fill.alpha=0.2)

  24. ellipse = FALSE,

  25. var.labels = colnames(x), #变量标签(用于绘图的对角线)通过字符串可控制对角线标签位置

  26. #diagonal 绘图对角线面板的内容。如果对角线=true,则绘制自适应核密度估计,

  27. #如果存在分组,则分别为每个组绘制。对角线=false 取消对角线条目。可为:

  28. #diagonal = list(method="adaptiveDensity", bw=bw.nrd0, adjust=1, kernel=dnorm, na.rm=TRUE) #核密度图

  29. #diagonal=list(method="density", bw="nrd0", adjust=1, kernel="gaussian", na.rm=TRUE) #非自适应核密度估计

  30. #diagonal=list(method ="histogram", breaks="FD") #直方图 忽略分组

  31. #diagonal=list(method="boxplot") #箱线图

  32. #diagonal=list(method="qqplot") #normal QQ plot QQ图

  33. #diagonal=list(method="oned") #倾斜于对角线的地毯图

  34. diagonal = TRUE,

  35. plot.points = TRUE, #如果为真,则在每个非对角面板中绘制点。

  36. groups = NULL, #将数据分组的因子或其他变量;用不同的颜色和打印字符打印组。

  37. by.groups = TRUE, #如果为真,则默认值、回归线和平滑将按组匹配。

  38. #use 如果“complete.obs”(默认),则省略缺少数据的案例;

  39. #如果“pairwise.complete.obs”),则在绘图的每个面板中使用所有有效案例。

  40. use = c("complete.obs", "pairwise.complete.obs"),

  41. #col 点的颜色;默认为从第二种颜色开始的轮盘。

  42. #Regline和Smooth的颜色与第一组点的颜色相同,但可以在Regline和Smooth参数中进行更改。

  43. col = carPalette()[-1], #col = c("red", "green3", "blue")

  44. pch = 1:n.groups, #为点绘制形状;默认为按顺序绘制字符(请参见par)。 pch = c(15,16,17)

  45. cex = par("cex"), #绘制点的相对大小

  46. cex.axis = par("cex.axis"), #坐标轴标签的 相对大小

  47. cex.labels = NULL, #对角线上标签的相对大小

  48. cex.main = par("cex.main"), #主标题的相对大小(如果有)

  49. row1attop = TRUE, #如果为true(默认值),第一行位于顶部,false则颠倒。

  50. ...)

为了方便理解,这里使用基础包中的mtcars数据,边解说,边绘图。

一睹为快

先一睹为快,看最代码和图:

 
  1. car::spm(~mpg + hp + wt, data = mtcars, groups = mtcars$gear, by.groups = F,

  2. smooth=list(lty.smooth=2,lwd.smooth = 3, col.smooth="red", spread = T, lty.spread=3, lwd.spread=2),

  3. diagonal = list(method="boxplot"), regLine=list(method =lm,lty=1,lwd=2,col="black"),

  4. legend = list(coords= "topleft"),

  5. var.labels = c("\n\n\nMPG\n(pg/ml)", "\n\n\nHP\n(μmol/L)", "\n\n\nWT\n(pg/ml)"),

  6. cex = 1, cex.labels = 1.5, cex.axis = 1.5,

  7. pch = c(16,16,16), col = c("red", "green3", "blue"), row1attop = T )

先加载程序包:

library(car)

1、数据添加

两种方式,1是用formula 格式指定数据框中的数据, 2是直接矩阵或者数据框:

 
  1. car::scatterplotMatrix(mtcars[c("mpg", hp, wt)])

  2. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")])

结果如图:

 

2、smooth 参数

平滑拟合曲线参数,False不绘制拟合曲线,True绘制,不分组则按全体拟合,分组则按分组拟合。

car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")], smooth = F)

可以通过list修改拟合曲线参数。默认True时,拟合曲线参数为:

smooth=list(smoother=loessLine, spread=TRUE, lty.smooth=1, lwd.smooth=1.5, lty.spread=3, lwd.spread=1)

可以更改添加的线条的平滑度、线条类型、宽度和颜色以及添加平滑度参数等。

smoother表示拟合曲线方法,有:loessLine、gamline、quantregLine,各种方法的拟合曲线原理公式 我是不知道了,各位看官如果知道,请给我说下。

Spread 表示是否绘制置信区间??,我猜的,反正False时,两条边上的曲线没了。如图:

car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")], smooth = list(spread = F))

lty.smooth, lty.spread 修改曲线的线条类型,  lwd.smooth和lwd.spread修改曲线的宽度

 
  1. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  2. smooth = list(spread = T, lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2))

3、id 控制点标识

如果为false(默认),则不标识任何点;可以是 ShowLabels 函数的命名参数列表;true相当于,

id = list(method=“mahal”,n=2,cex=1,location=“lr”)

它用最大的maha标识2个点(在每个组中,如果为by.groups=true)与数据中心的距离;不允许使用列表(method=“identify”)进行交互点标识。这个我没改过,直接用默认True,标出离群点,如下图:

 
  1. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")], id = T,

  2. smooth = list(spread = T, lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2))

 后续很多参数需要用到分组,这里先把分组讲了

4、groups分组

         和许多其他绘图函数一样,这里需要传入一个分组数据:groups = mtcars$gear

 
  1. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  2. smooth = list(spread = T, lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2),

  3. id = T, groups = mtcars$gear )

很多时候,我们不需要将每组进行拟合,而是想将整体数据进行拟合,这时我们需要修改参数:

by.groups = F, (Flase表示不需要每组进行拟合)

 
  1. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  2. smooth = list(spread = T, lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2),

  3. id = T, groups = mtcars$gear, by.groups = F )

 

上面点的颜色和形状太难看了,需要修改下参数如下:

pch = c(15,16,17),  #由于我们分了3组,所有这里只需要3个数据

col = c("red", "green3", "blue")

 
  1. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  2. smooth = list(spread = T, col.smooth = "black", col.spread = "black",

  3. lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2),

  4. id = T, groups = mtcars$gear, by.groups = F,

  5. pch = c(15,16,17),col = c("red", "green3", "blue"))

 

5、diagonal 对角线参数设置

可依次取为;
 

 
  1. diagonal=list(method="adaptiveDensity", bw=bw.nrd0, adjust=1, kernel=dnorm, na.rm=TRUE) #核密度图

  2. diagonal=list(method="density", bw="nrd0", adjust=1, kernel="gaussian", na.rm=TRUE) #非自适应核密度估计

  3. diagonal=list(method ="histogram", breaks="FD") #直方图 忽略分组

  4. diagonal=list(method="boxplot") #箱线图

  5. diagonal=list(method="qqplot") #normal QQ plot QQ图

  6. diagonal=list(method="oned") #倾斜于对角线的地毯图

 
  1. #核密度图

  2. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  3. smooth = list(spread = T, col.smooth = "black", col.spread = "black", lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2),

  4. id = T, groups = mtcars$gear, by.groups = F,

  5. pch = c(15,16,17),col = c("red", "green3", "blue"),

  6. diagonal=list(method="adaptiveDensity", bw=bw.nrd0, adjust=1, kernel=dnorm, na.rm=TRUE) )

  7. #非自适应核密度估计

  8. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  9. smooth = list(spread = T, col.smooth = "black", col.spread = "black", lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2),

  10. id = T, groups = mtcars$gear, by.groups = F,

  11. pch = c(15,16,17),col = c("red", "green3", "blue"),

  12. diagonal=list(method="density", bw="nrd0", adjust=1, kernel="gaussian", na.rm=TRUE) )

  13. #直方图 忽略分组

  14. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  15. smooth = list(spread = T, col.smooth = "black", col.spread = "black", lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2),

  16. id = T, groups = mtcars$gear, by.groups = F,

  17. pch = c(15,16,17),col = c("red", "green3", "blue"),

  18. diagonal=list(method ="histogram", breaks="FD") )

  19. #箱线图

  20. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  21. smooth = list(spread = T, col.smooth = "black", col.spread = "black", lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2),

  22. id = T, groups = mtcars$gear, by.groups = F,

  23. pch = c(15,16,17),col = c("red", "green3", "blue"),

  24. diagonal=list(method="boxplot"))

  25. #normal QQ plot QQ图

  26. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  27. smooth = list(spread = T, col.smooth = "black", col.spread = "black", lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2),

  28. id = T, groups = mtcars$gear, by.groups = F,

  29. pch = c(15,16,17),col = c("red", "green3", "blue"),

  30. diagonal=list(method="qqplot") )

  31. #倾斜于对角线的地毯图

  32. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  33. smooth = list(spread = T, col.smooth = "black", col.spread = "black", lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2),

  34. id = T, groups = mtcars$gear, by.groups = F,

  35. pch = c(15,16,17),col = c("red", "green3", "blue"),

  36. diagonal=list(method="oned") )

图依次是:

6、legend 图例

legend 参数是我花费时间最多的,百度不到相关说明,help帮助英文看了也很疑惑,使用不来。最终下了car报源码,自己看了才明白,可以调整其在第一个对角线图形中的位置。如:legend = list(coords= "topleft") (左上角)

位置有9个选择:

c("bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right", "center")
 
  1. car::scatterplotMatrix(mtcars[c("mpg", "hp", "wt")],

  2. smooth = list(spread = T, col.smooth = "black", col.spread = "black", lty.smooth=2, lwd.smooth=3, lty.spread=3, lwd.spread=2),

  3. id = T, groups = mtcars$gear, by.groups = F,

  4. pch = c(15,16,17),col = c("red", "green3", "blue"),

  5. diagonal=list(method="boxplot"),

  6. legend = list(coords= "topleft"))

 

附scatterplotMatrix函数源码:

 
  1. # fancy scatterplot matrices (J. Fox)

  2.  
  3. # 2010-09-04: J. Fox: changed color choice

  4. # 2010-09-16: fixed point color when col is length 1

  5. # 2011-03-08: J. Fox: changed col argument

  6. # 2012-04-18: J. Fox: fixed labels argument in scatterplotMatrix.formula()

  7. # 2012-09-12: J. Fox: smoother now given as function

  8. # 2012-09-19: J. Fox: restored smooth and span args for backwards compatibility

  9. # 2013-02-08: S. Weisberg: bug-fix for showLabels with groups

  10. # 2013-08-26: J. Fox: added use argument

  11. # 2014-08-07: J. Fox: plot univariate distributions by group (except for histogram)

  12. # 2014-08-17: J. Fox: report warning rather than error if not enough points in a group

  13. # to compute density

  14. # 2014-09-04: J. Fox: empty groups produce warning rather than error

  15. # 2017-02-14: J. Fox: consolidated smooth, id, legend, and ellipse arguments

  16. # 2017-02-17: S. Weisberg, more changes to arguments

  17. # 2017-02-19: J. Fox: bug fixes and improvement to col argument

  18. # 2017-04-18; S. Weisberg fixed bug in handling id=FALSE with matrix/data frame input.

  19. # 2017-04-18; S. Weisberg changed the default for by.groups to TRUE

  20. # 2017-04-20: S. Weisberg fixed bug with color handling

  21. # 2017-04-20: S. Weisberg the default diagonal is now adaptiveDensity using adaptiveKernel fn

  22. # diagonal argument is now a list similar to regLine and smooth

  23. # changed arguments and updated man page

  24. # 2017-05-08: S. Weisberg changed col=carPalette()

  25. # 2017-06-22: J. Fox: eliminated extraneous code for defunct labels argument; small cleanup

  26. # 2017-12-07: J. Fox: added fill, fill.alpha subargs to ellipse arg, suggestion of Michael Friendly.

  27. # 2018-02-09: S. Weisberg removed the transform and family arguments from the default method

  28. # 2018-04-02: J. Fox: warning rather than error for too few colors.

  29. # 2018-04-12: J. Fox: clean up handling of groups arg.

  30.  
  31. scatterplotMatrix <- function(x, ...){

  32. UseMethod("scatterplotMatrix")

  33. }

  34.  
  35. scatterplotMatrix.formula <- function (formula, data=NULL, subset, ...) {

  36. na.save <- options(na.action=na.omit)

  37. on.exit(options(na.save))

  38. na.pass <- function(dframe) dframe

  39. m <- match.call(expand.dots = FALSE)

  40. if (is.matrix(eval(m$data, sys.frame(sys.parent()))))

  41. m$data <- as.data.frame(data)

  42. m$id <- m$formula <- m$... <- NULL

  43. m$na.action <- na.pass

  44. m[[1]] <- as.name("model.frame")

  45. if (!inherits(formula, "formula") | length(formula) != 2)

  46. stop("invalid formula")

  47. rhs <- formula[[2]]

  48. if ("|" != deparse(rhs[[1]])){

  49. groups <- FALSE

  50. }

  51. else{

  52. groups <- TRUE

  53. formula <- as.character(formula)

  54. formula <- as.formula(sub("\\|", "+", formula))

  55. }

  56. m$formula <-formula

  57. if (missing(data)){

  58. X <- na.omit(eval(m, parent.frame()))

  59. # if (is.null(labels)) labels <- gsub("X", "", row.names(X))

  60. }

  61. else{

  62. X <- eval(m, parent.frame())

  63. # if (is.null(labels)) labels <- rownames(X)

  64. }

  65. if (!groups) scatterplotMatrix(X, ...)

  66. else{

  67. ncol<-ncol(X)

  68. scatterplotMatrix.default(X[, -ncol], groups=X[, ncol], ...)

  69. }

  70. }

  71.  
  72.  
  73. scatterplotMatrix.default <-

  74. function(x, smooth=TRUE, id=FALSE, legend=TRUE,

  75. regLine=TRUE, ellipse=FALSE,

  76. var.labels=colnames(x),

  77. diagonal=TRUE,

  78. plot.points=TRUE,

  79. groups=NULL, by.groups=TRUE,

  80. use=c("complete.obs", "pairwise.complete.obs"),

  81. col=carPalette()[-1],

  82. pch=1:n.groups,

  83. cex=par("cex"), cex.axis=par("cex.axis"),

  84. cex.labels=NULL, cex.main=par("cex.main"), row1attop=TRUE, ...){

  85. transform <- FALSE

  86. # family <- "bcPower"

  87. force(col)

  88. # n.groups <- if(by.groups) length(levels(groups)) else 1

  89. if(isFALSE(diagonal)) diagonal <- "none" else {

  90. diagonal.args <- applyDefaults(diagonal, defaults=list(method="adaptiveDensity"), type="diag")

  91. diagonal <- if(!isFALSE(diagonal.args)) diagonal.args$method

  92. diagonal.args$method <- NULL

  93. }

  94. # regLine; use old arguments reg.line, lty and lwd

  95. regLine.args <- applyDefaults(regLine, defaults=list(method=lm, lty=1, lwd=2,

  96. col=col), type="regLine")

  97. if(!isFALSE(regLine.args)) {

  98. reg.line <- regLine.args$method

  99. lty <- regLine.args$lty

  100. lwd <- regLine.args$lwd

  101. } else reg.line <- "none"

  102. # setup smoother, now including spread

  103. n.groups <- if(is.null(groups)) 1

  104. else {

  105. if (!is.factor(groups)) groups <- as.factor(groups)

  106. length(levels(groups))

  107. }

  108. smoother.args <- applyDefaults(smooth, defaults=list(smoother=loessLine,

  109. spread=(n.groups)==1, col=col, lty.smooth=2, lty.spread=4), type="smooth")

  110. if (!isFALSE(smoother.args)) {

  111. # check for an argument 'var' in smoother.args.

  112. if(!is.null(smoother.args$var)) smoother.args$spread <- smoother.args$var

  113. # end change

  114. smoother <- smoother.args$smoother

  115. spread <- if(is.null(smoother.args$spread)) TRUE else smoother.args$spread

  116. smoother.args$spread <- smoother.args$smoother <- NULL

  117. if(n.groups==1) smoother.args$col <- col[1]

  118. }

  119. else smoother <- "none"

  120. # setup id

  121. id <- applyDefaults(id, defaults=list(method="mahal", n=2, cex=1, col=col, location="lr"), type="id")

  122. if (is.list(id) && "identify" %in% id$method) stop("interactive point identification not permitted")

  123. if (isFALSE(id)){

  124. id.n <- 0

  125. id.method <- "mahal"

  126. labels <- id.cex <- id.col <- id.location <- NULL

  127. }

  128. else{

  129. labels <- if(!is.null(id$labels)) id$labels else row.names(x)

  130. id.method <- id$method

  131. id.n <- id$n

  132. id.cex <- id$cex

  133. id.col <- id$col

  134. id.location <- id$location

  135. }

  136. if (is.null(labels)) labels <- as.character(seq(length.out=nrow(x)))

  137. legend <- applyDefaults(legend, defaults=list(coords=NULL), type="legend")

  138. if (!(isFALSE(legend) || missing(groups))){

  139. legend.plot <- TRUE

  140. legend.pos <- legend$coords

  141. }

  142. else {

  143. legend.plot <- FALSE

  144. legend.pos <- NULL

  145. }

  146. # ellipse

  147. ellipse <- applyDefaults(ellipse, defaults=list(levels=c(0.5, 0.95), robust=TRUE, fill=TRUE, fill.alpha=0.2), type="ellipse")

  148. if (isFALSE(ellipse)){

  149. levels <- NULL

  150. robust <- NULL

  151. }

  152. else{

  153. levels <- ellipse$levels

  154. robust <- ellipse$robust

  155. fill <- ellipse$fill

  156. fill.alpha <- ellipse$fill.alpha

  157. ellipse <- TRUE

  158. }

  159. # pre 2017 code follows

  160. # family <- match.arg(family)

  161. use <- match.arg(use)

  162. na.action <- if (use == "complete.obs") na.omit else na.pass

  163. if (!(missing(groups))){

  164. x <- na.action(data.frame(groups, labels, x, stringsAsFactors=FALSE))

  165. # groups <- as.factor(as.character(x[, 1]))

  166. groups <- x$groups

  167. # if (!is.factor(groups)) groups <- as.factor(as.character(x[,1]))

  168. labels <- x[, 2]

  169. x <- x[, -(1:2)]

  170. }

  171. else {

  172. x <- na.action(data.frame(labels, x, stringsAsFactors=FALSE))

  173. labels <- x[, 1]

  174. x <- x[, -1]

  175. id.col <- id.col[1]

  176. }

  177. legendPlot <- function(position="topright"){

  178. usr <- par("usr")

  179. legend(position, bg="white",

  180. legend=levels(groups), pch=pch, col=col[1:n.groups],

  181. cex=cex)

  182. }

  183. do.legend <- legend.plot

  184. ####### diagonal panel functions

  185. # The following panel function adapted from Richard Heiberger

  186. panel.adaptiveDensity <- function(x, ...){

  187. args <- applyDefaults(diagonal.args,

  188. defaults=list(bw=bw.nrd0, adjust=1, kernel=dnorm, na.rm=TRUE))

  189. if (n.groups > 1){

  190. levs <- levels(groups)

  191. for (i in 1:n.groups){

  192. xx <- x[levs[i] == groups]

  193. dens.x <- try(adaptiveKernel(xx, adjust = args$adjust, na.rm=args$na.rm,

  194. bw=args$bw, kernel=args$kernel), silent=TRUE)

  195. if (!inherits(dens.x, "try-error")){

  196. lines(dens.x$x, min(x, na.rm=TRUE) + dens.x$y *

  197. diff(range(x, na.rm=TRUE))/diff(range(dens.x$y, na.rm=TRUE)), col=col[i])

  198. }

  199. else warning("cannot estimate density for group ", levs[i], "\n",

  200. dens.x, "\n")

  201. rug(xx, col=col[i])

  202. }

  203. }

  204. else {

  205. dens.x <- adaptiveKernel(x, adjust = args$adjust, na.rm=args$na.rm,

  206. bw=args$bw, kernel=args$kernel)

  207. lines(dens.x$x, min(x, na.rm=TRUE) + dens.x$y * diff(range(x, na.rm=TRUE))/diff(range(dens.x$y, na.rm=TRUE)), col=col[1])

  208. rug(x)

  209. }

  210. if (do.legend) legendPlot(position=if (is.null(legend.pos)) "topright" else legend.pos)

  211. do.legend <<- FALSE

  212. }

  213. #

  214. panel.density <- function(x, ...){

  215. args <- applyDefaults(diagonal.args,

  216. defaults=list(bw="nrd0", adjust=1, kernel="gaussian", na.rm=TRUE))

  217. if (n.groups > 1){

  218. levs <- levels(groups)

  219. for (i in 1:n.groups){

  220. xx <- x[levs[i] == groups]

  221. dens.x <- try(density(xx, adjust = args$adjust, na.rm=args$na.rm,

  222. bw=args$bw, kernel=args$kernel), silent=TRUE)

  223. if (!inherits(dens.x, "try-error")){

  224. lines(dens.x$x, min(x, na.rm=TRUE) + dens.x$y *

  225. diff(range(x, na.rm=TRUE))/diff(range(dens.x$y, na.rm=TRUE)), col=col[i])

  226. }

  227. else warning("cannot estimate density for group ", levs[i], "\n",

  228. dens.x, "\n")

  229. rug(xx, col=col[i])

  230. }

  231. }

  232. else {

  233. dens.x <- density(x, adjust = args$adjust, na.rm=args$na.rm,

  234. bw=args$bw, kernel=args$kernel)

  235. lines(dens.x$x, min(x, na.rm=TRUE) + dens.x$y * diff(range(x, na.rm=TRUE))/diff(range(dens.x$y, na.rm=TRUE)), col=col[1])

  236. rug(x)

  237. }

  238. if (do.legend) legendPlot(position=if (is.null(legend.pos)) "topright" else legend.pos)

  239. do.legend <<- FALSE

  240. }

  241. panel.histogram <- function(x, ...){

  242. par(new=TRUE)

  243. args <- applyDefaults(diagonal.args, defaults=list(breaks="FD"))

  244. h.col <- col[1]

  245. if (h.col == "black") h.col <- "gray"

  246. hist(x, main="", axes=FALSE, breaks=args$breaks, col=h.col)

  247. if (do.legend) legendPlot(position=if (is.null(legend.pos)) "topright" else legend.pos)

  248. do.legend <<- FALSE

  249. }

  250. panel.boxplot <- function(x, ...){

  251. b.col <- col[1:n.groups]

  252. b.col[b.col == "black"] <- "gray"

  253. par(new=TRUE)

  254. if (n.groups == 1) boxplot(x, axes=FALSE, main="", col=col[1])

  255. else boxplot(x ~ groups, axes=FALSE, main="", col=b.col)

  256. if (do.legend) legendPlot(position=if (is.null(legend.pos)) "topright" else legend.pos)

  257. do.legend <<- FALSE

  258. }

  259. # The following panel function adapted from Richard Heiberger

  260. panel.oned <- function(x, ...) {

  261. range <- range(x, na.rm=TRUE)

  262. delta <- diff(range)/50

  263. y <- mean(range)

  264. if (n.groups == 1) segments(x - delta, x, x + delta, x, col = col[1])

  265. else {

  266. segments(x - delta, x, x + delta, x, col = col[as.numeric(groups)])

  267. }

  268. if (do.legend) legendPlot(position=if (is.null(legend.pos)) "bottomright" else legend.pos)

  269. do.legend <<- FALSE

  270. }

  271. panel.qqplot <- function(x, ...){

  272. par(new=TRUE)

  273. if (n.groups == 1) qqnorm(x, axes=FALSE, xlab="", ylab="", main="", col=col[1])

  274. else qqnorm(x, axes=FALSE, xlab="", ylab="", main="", col=col[as.numeric(groups)])

  275. qqline(x, col=col[1])

  276. if (do.legend) legendPlot(position=if (is.null(legend.pos)) "bottomright" else legend.pos)

  277. do.legend <<- FALSE

  278. }

  279. panel.blank <- function(x, ...){

  280. if (do.legend) legendPlot(if (is.null(legend.pos)) "topright" else legend.pos)

  281. do.legend <<- FALSE

  282. }

  283. which.fn <- match(diagonal,

  284. c("adaptiveDensity", "density", "boxplot", "histogram", "oned", "qqplot", "none"))

  285. if(is.na(which.fn)) stop("incorrect name for the diagonal argument, see ?scatterplotMatrix")

  286. diag <- list(panel.adaptiveDensity, panel.density, panel.boxplot, panel.histogram, panel.oned,

  287. panel.qqplot, panel.blank)[[which.fn]]

  288. groups <- as.factor(if(missing(groups)) rep(1, length(x[, 1])) else groups)

  289. counts <- table(groups)

  290. if (any(counts == 0)){

  291. levels <- levels(groups)

  292. warning("the following groups are empty: ", paste(levels[counts == 0], collapse=", "))

  293. groups <- factor(groups, levels=levels[counts > 0])

  294. }

  295. # n.groups <- length(levels(groups))

  296. if (n.groups > length(col)) {

  297. warning("number of groups exceeds number of available colors\n colors are recycled")

  298. col <- rep(col, n.groups)

  299. }

  300. if (length(col) == 1) col <- rep(col, 3)

  301. labs <- labels

  302. pairs(x, labels=var.labels,

  303. cex.axis=cex.axis, cex.main=cex.main, cex.labels=cex.labels, cex=cex,

  304. diag.panel=diag, row1attop = row1attop,

  305. panel=function(x, y, ...){

  306. for (i in 1:n.groups){

  307. subs <- groups == levels(groups)[i]

  308. if (plot.points) points(x[subs], y[subs], pch=pch[i], col=col[if (n.groups == 1) 1 else i], cex=cex)

  309. if (by.groups){

  310. if (is.function(smoother)) smoother(x[subs], y[subs], col=smoother.args$col[i],

  311. log.x=FALSE, log.y=FALSE, spread=spread, smoother.args=smoother.args)

  312. if (is.function(reg.line)) regLine(reg.line(y[subs] ~ x[subs]), lty=lty, lwd=lwd, col=regLine.args$col[i])

  313. if (ellipse) dataEllipse(x[subs], y[subs], plot.points=FALSE,

  314. levels=levels, col=col[i], robust=robust, lwd=1,

  315. fill=fill, fill.alpha=fill.alpha)

  316. showLabels(x[subs], y[subs], labs[subs], method=id.method,

  317. n=id.n, col=col[i], cex=id.cex, location=id.location,

  318. all=list(labels=labs, subs=subs))

  319. }

  320. }

  321. if (!by.groups){

  322. if (is.function(reg.line)) abline(reg.line(y ~ x), lty=lty, lwd=lwd, col=regLine.args$col[1])

  323. if (is.function(smoother)) smoother(x, y, col=col[1],

  324. log.x=FALSE, log.y=FALSE, spread=spread, smoother.args=smoother.args)

  325. if (ellipse) dataEllipse(x, y, plot.points=FALSE, levels=levels, col=smoother.args$col,

  326. robust=robust, lwd=1, fill=fill, fill.alpha=fill.alpha)

  327. showLabels(x, y, labs, method=id.method,

  328. n=id.n, col=id.col, location=id.location, cex=id.cex)

  329. }

  330. }, ...

  331. )

  332. }

  333.  
  334. spm <- function(x, ...){

  335. scatterplotMatrix(x, ...)

  336. }

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值