Intermediate Drawing - Scatter

In this chapter we focus on inter-drawing method for displaying bivariate and multivariate relationships relations
11.1 Scatter
Scatter can be used to describe the relationship between two continuous variables.
R scattergram created in the base function is plot (x, y), where, x and y are numerical vector representing the (X, y) of the dot pattern
code sample

attach(mtcars)
plot(wt,mpg,
     main="Basic Scatter plot of MPG vs. Weight",
     xlab="Car Weight (1bs/1000)",
     ylab = "Miles Per Gallon ",
     pch=19)
abline(lm(mpg~wt),col="red",lwd=2,lty=1)
#其中lowess()函数用来添加一条平滑曲线。
#该平滑曲线是一种基于局部加权多项式回归的非参数方法(可以看做线性回归的一种方式)
#lowess()返回回归后的$x值和$y值
lines(lowess(wt,mpg),col="blue",lwd=2,lty=2)

Here Insert Picture Description

scatterplot car package () function enhances many features scattergram
Scatterplot () can easily draw scattergram and curve fitting can be added, and the boundary boxplot confidence ellipse, but also by drawing and subsets interactively identification point
code sample

library(car)
attach(mtcars)
#参数legend可以为图形添加图例,参数boxplots="xy"可以添加x轴变量及y轴变量的箱线图
#参数id=TRUE它标识(在每个组中)Mahalanobis距离数据中心最大的两个点,在car中的所有图形函数中,id的默认行为并不相同,因为使用的方法取决于绘图的类型。
scatterplot(mpg~wt|cyl,data=mtcars,lwd=2,
            main="Basic Scatter plot of MPG vs. Weight by # Cylinders",
            xlab="Weight of Car (lbs/1000)",
            ylab="Miles Per Gallon",
            legend=TRUE,
            boxplots="xy",
            id=TRUE
            )

Here Insert Picture Description
11.1.1 scatterplot matrix
pairs () function can be created based on a scatter plot matrix.
The sample code

pairs(~mpg + disp + drat + wt, data=mtcars,
      main="Basic Scatter Plot Matrix")

Here Insert Picture Description
We can see the binary relation between all the variables from the graph.
Note that the main diagonal above and six below the scatter plot is the same. By parameters, is only displayed on the graphic or lower triangular, e.g. upper.panel = NULL will only generate a lower triangular pattern

scatterplotMatrix car package () function can also be generated scatter plot matrix, and has a lower optional
1, drawn to a scatter plot matrix factor conditions;
2, comprising a linear curve fitting and smoothing;
3, the main FIG diagonal line box is placed, or the density histogram in FIG.
4, the addition of the boundary of each cell is the axis of FIG shall

library(car)
#参数smooth=list(method=loessLine,spread=FALSE,lty.smooth=2,col.smooth='red')设定了平滑曲线的类型,颜色及线型,
#spread = FALSE表示不添加展示分散度和对称信息的直线
#diagonal可以设定一系列关于对角线图形的设置,比如选择显示密度图还是直方图等,diagonal=FALSE则不显示对角线图形
#regLine可以设定拟合直线的相关设定
scatterplotMatrix(~mpg + disp + drat + wt, data=mtcars,
                  main="Scatter Plot Matrix via car Pachage",
                  regLine=list(mod=lm,col="green",lty=1),
                  smooth=list(method=loessLine,spread=FALSE,lty.smooth=2,col.smooth='red'),
                  diagonal=TRUE)

Here Insert Picture Description
R language provides many other ways to create a scatterplot matrix e.g.
glus package cpars () function, TeachingDemos package pairs2 () function, HH package xysplom () function, ResourceSelection package kepairs () function pairs.mod SMPracticals package () function

Summary:
1, drawing basis functions can be used scattergram plot () function
2, car package Scatterplot () function is also used to draw scattergram, but relative to Plot () function, it can add fitting curve , accessory fitting a straight line, the boundary line of the box like FIG. Be drawn scatter plot of the high-level functions.
3, the base matrix is plotted as a function scattergram pairs () function
4, car package scatterplotMatrix () function can also draw a scatter plot matrix, but with respect to the pairs () function, it can be added fitting curve, fitting a straight line, axis shall FIG, and draw box plots, a histogram density map or on the main diagonal. Be drawn high-level functions scatterplot matrices.

Published 39 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42712867/article/details/100129713