本章主要讲解中级绘图函数,主要包括:

1.散点图

下图为添加了最佳拟合曲线的散点图

attach(mtcars)                                                     
plot(wt, mpg, 
     main="Basic Scatterplot of MPG vs. Weight",       
     xlab="Car Weight (lbs/1000)", 
     ylab="Miles Per Gallon ", pch=19)
abline(lm(mpg ~ wt), col="red", lwd=2, lty=1)            
lines(lowess(wt, mpg), col="blue", lwd=2, lty=2)

detach(mtcars)

注意上图中使用了lowess函数来平滑曲线loess函数拥有同样功能,具体平滑计算参见Cleveland 1981 car包中的scatterplot函数增强了对应功能:

# Scatter plot with fit lines by group
library(car) 
scatterplot(mpg ~ wt | cyl, data=mtcars, lwd=2,
            main="Scatter Plot of MPG vs. Weight by # Cylinders", 
            xlab="Weight of Car (lbs/1000)", 
            ylab="Miles Per Gallon", id.method="identify",
            legend.plot=TRUE, labels=row.names(mtcars), 
            legend.coords="topright",
            boxplots="xy")

library(car)
scatterplotMatrix(~mpg+disp+drat+wt,data = mtcars,
                  spread=FALSE,smoother.arg=list(lty=2),
                  main="Scatter Plot Matrix via car Package")

# spread设置是否添加分散度和对称信息曲线
# smooother设置平滑曲线类型

用于绘制散点图矩阵的库及函数:

2.散点图矩阵

当点数较多较密集时可以使用高密度散点图模式来绘制散点图:

set.seed(1234)
n=10000
c1=matrix(rnorm(n,mean = 0,sd = .5),ncol = 2)
c2=matrix(rnorm(n,mean = 3,sd = 2),ncol = 2)
mydata=rbind(c1,c2)
mydata=as.data.frame(mydata)
names(mydata)=c("x","y")
with(mydata,
     plot(x,y,pch=19,main = "Scatter Plot with 1000 Observations"))

高密度散点图的变形模式:

with(mydata,
     smoothScatter(x,y,main="Scatter Plot Colored by Smoothed Densities"))

library(hexbin)
with(mydata,{
     bin=hexbin(x,y,xbins = 50)
     plot(bin,main="Hexagonal Binning with  10,000 Observations")
     })

3.三维散点图

同时我们可以将散点图应用于三维的数据格式,如下:

library(scatterplot3d)
attach(mtcars)
scatterplot3d(wt,disp,mpg,
              main = "Basic 3D Scatter Plot")

scatterplot3d(wt,disp,mpg,
              pch=16,
              highlight.3d = TRUE,
              type = "h",
              main = "3D Scatter Plot with Vertical Lines and Regression Plane")
s3d <-scatterplot3d(wt, disp, mpg,
                    pch=16,
                    highlight.3d=TRUE,
                    type="h",
                    main="3D Scatter Plot with Vertical Lines and Regression Plane")
fit=lm(mpg~wt+disp)
s3d$plane3d(fit)

detach(mtcars)

当需要添加旋转观察模式时,我们可以选用如下两种函数进行交互:

library(rgl)
attach(mtcars)
plot3d(wt,disp,mpg,col="red",size=5)
library(car)
attach(mtcars)
scatter3d(wt,disp,mpg)

4.气泡图

attach(mtcars)
r=sqrt(disp/pi)
symbols(wt,mpg,circles = r,inches = 0.30,
        fg="white",bg="lightblue",
        main = "Bubble Plot with point size proportional to displacement",
        ylab = "Miles Per Gallon",
        xlab = "Weight of Car (lbs/1000)")
text(wt,mpg,rownames(mtcars),cex = 0.6)

detach(mtcars)

注意:inchs参数控制着气泡大小与数据比例,选择合适的比例可以避免可能的混乱。

5.其他绘图

5.1.绘制散点图

par(mfrow=c(1,2))
t1 <- subset(Orange, Tree==1)
plot(t1$age, t1$circumference,
     xlab="Age (days)",
     ylab="Circumference (mm)",
     main="Orange Tree 1 Growth")
plot(t1$age, t1$circumference,
     xlab="Age (days)",
     ylab="Circumference (mm)",
     main="Orange Tree 1 Growth",
     type="b")

Orange$Tree <- as.numeric(Orange$Tree)
ntrees <- max(Orange$Tree)
xrange <- range(Orange$age)
yrange <- range(Orange$circumference)
plot(xrange, yrange,
     type="n",
     xlab="Age (days)",
     ylab="Circumference (mm)"
)
colors <- rainbow(ntrees)
linetype <- c(1:ntrees)
plotchar <- seq(18, 18+ntrees, 1)
for (i in 1:ntrees) {
  tree <- subset(Orange, Tree==i)
  lines(tree$age, tree$circumference,
        type="b",
        lwd=2,
        lty=linetype[i],
        col=colors[i],
        pch=plotchar[i]
  )
}
title("Tree Growth", "example of line plot")
legend(xrange[1], yrange[2],
       1:ntrees,
       cex=0.8,
       col=colors,
       pch=plotchar,
       lty=linetype,
       title="Tree"
)                                          

5.2.绘制相关图

options(digits = 2)
cor(mtcars)
##        mpg   cyl  disp    hp   drat    wt   qsec    vs     am  gear   carb
## mpg   1.00 -0.85 -0.85 -0.78  0.681 -0.87  0.419  0.66  0.600  0.48 -0.551
## cyl  -0.85  1.00  0.90  0.83 -0.700  0.78 -0.591 -0.81 -0.523 -0.49  0.527
## disp -0.85  0.90  1.00  0.79 -0.710  0.89 -0.434 -0.71 -0.591 -0.56  0.395
## hp   -0.78  0.83  0.79  1.00 -0.449  0.66 -0.708 -0.72 -0.243 -0.13  0.750
## drat  0.68 -0.70 -0.71 -0.45  1.000 -0.71  0.091  0.44  0.713  0.70 -0.091
## wt   -0.87  0.78  0.89  0.66 -0.712  1.00 -0.175 -0.55 -0.692 -0.58  0.428
## qsec  0.42 -0.59 -0.43 -0.71  0.091 -0.17  1.000  0.74 -0.230 -0.21 -0.656
## vs    0.66 -0.81 -0.71 -0.72  0.440 -0.55  0.745  1.00  0.168  0.21 -0.570
## am    0.60 -0.52 -0.59 -0.24  0.713 -0.69 -0.230  0.17  1.000  0.79  0.058
## gear  0.48 -0.49 -0.56 -0.13  0.700 -0.58 -0.213  0.21  0.794  1.00  0.274
## carb -0.55  0.53  0.39  0.75 -0.091  0.43 -0.656 -0.57  0.058  0.27  1.000
library(corrgram)
corrgram(mtcars,order = TRUE,
         lower.panel = panel.shade,
         upper.panel = panel.pie,
         text.panel = panel.txt,
         main="Corrgram of mtcats intercorrelations")

corrgram(mtcars,order = TRUE,
         lower.panel = panel.ellipse,
         upper.panel = panel.pts,
         text.panel = panel.txt,
         diag.panel = panel.minmax,
         main="Corrgram of mtcats data using scatter plots and ellipse")

corrgram(mtcars,order = TRUE,
         lower.panel = panel.shade,
         upper.panel = NULL,
         text.panel = panel.txt,
         main="Car Mileage Data(unsorted)")

相关图参数设置:

  • order:是否按照主成分分析结果对变量排序
  • panel:绘制图形类型
  • upper:上三角绘图类型
  • lower:下三角绘图类型
  • text:对角线标注类型
  • diag:给出最大最小标注

下面使用colorRampPallette函数来改变图形颜色:

library(corrgram)
cols=colorRampPalette(c("darkgoldenrod4","burlywood1","darkkhaki","darkgreen"))
corrgram(mtcars,order = TRUE,col.regions = cols,
         lower.panel = panel.shade,
         upper.panel = panel.conf,
         text.panel = panel.text,
         main="A Corrgram of a Different Color")

5.3 马赛克图

ftable(Titanic)
##                    Survived  No Yes
## Class Sex    Age                   
## 1st   Male   Child            0   5
##              Adult          118  57
##       Female Child            0   1
##              Adult            4 140
## 2nd   Male   Child            0  11
##              Adult          154  14
##       Female Child            0  13
##              Adult           13  80
## 3rd   Male   Child           35  13
##              Adult          387  75
##       Female Child           17  14
##              Adult           89  76
## Crew  Male   Child            0   0
##              Adult          670 192
##       Female Child            0   0
##              Adult            3  20
library(vcd)
mosaic(Titanic,shade = TRUE,legend=TRUE)

mosaic(~Class+Sex+Age+Survived,data = Titanic,shade=TRUE,legend=TRUE)