R Markdown

Answer for Programming Assignment_1 (PA1)

Realize Ex2.2 in Textbook, and Problem 2.5, 2.6 of Chapter 2

##Ex2.2 P45, Fisher Iris Data #####

library(MASS)
library(ade4)
## Warning: 程辑包'ade4'是用R版本4.2.2 来建造的
library(scatterplot3d)
data(iris)

#par(mar = c(0, 0, 0, 0))
pane1 <- function(X, Y) {
  XY <- cbind.data.frame(X, Y)
  s.class(XY, iris$Species, include.ori = F, add.p = T, clab = 1.5,
          col = c("blue", "black", "red"), cpoi = 2, csta = 0.5)
}

pairs(iris[, 1:4], panel = pane1) # Scatter

#detach(package:ade4)

par(mfrow = c(2, 2)); mar0 = c(3, 3, 1, 3)
scatterplot3d(iris[, 1], iris[, 2], iris[, 3], mar = mar0, 
              color = c("blue","black", "red")[iris$Species], pch = 19,xlab = "萼片长度",ylab = "萼片宽度",zlab = "花瓣长度")
scatterplot3d(iris[, 2], iris[, 3], iris[, 4], mar = mar0, 
              color = c("blue","black", "red")[iris$Species], pch = 19,xlab = "萼片宽度",ylab = "花瓣长度",zlab = "花瓣宽度")
scatterplot3d(iris[, 3], iris[, 4], iris[, 1], mar = mar0, 
              color = c("blue","black", "red")[iris$Species], pch = 19,xlab = "花瓣长度",ylab = "花瓣宽度",zlab = "萼片长度")
scatterplot3d(iris[, 4], iris[, 1], iris[, 2], mar = mar0, 
              color = c("blue","black", "red")[iris$Species], pch = 19,xlab = "花瓣宽度",ylab = "萼片长度",zlab = "萼片宽度")

#detach(package:scatterplot3d)

Problem 2.5 Iris (1)轮廓图,雷达图,气泡图; (2) 星图和脸谱图

library(ggplot2)
## Warning: 程辑包'ggplot2'是用R版本4.2.2 来建造的
library(GGally)
## Warning: 程辑包'GGally'是用R版本4.2.2 来建造的
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
Idata<-iris

# 轮廓图
# Package: plotrix, Fun: ladderplot
library(plotrix)
data.m = as.matrix(Idata[,1:4])
rownames(data.m) = Idata[,5]
ladderplot(data.m, col=rainbow(31), pch=21, lty=1)

data.m.setosa = as.matrix(Idata[1:50,1:4])
ladderplot(data.m.setosa, col=2, pch=21, lty=1)
legend(x="topright", legend= "Setosa", lty=1, col=2, box.col="grey1")

data.m.versicolor = as.matrix(Idata[51:100,1:4])
ladderplot(data.m.versicolor, col=3, pch=21, lty=1)
legend(x="topright", legend= "Versicolor", lty=1, col=3, box.col="grey1")

data.m.virginica = as.matrix(Idata[101:150,1:4])
ladderplot(data.m.virginica, col=4, pch=21, lty=1)
legend(x="topright", legend= "Virginica", lty=1, col=4, box.col="grey1")

# 雷达图
library(ggplot2)
library(ggiraphExtra)
## Warning: 程辑包'ggiraphExtra'是用R版本4.2.2 来建造的
ggRadar(data = Idata, aes(group = Species), alpha = 0)+
  theme(axis.text = element_text(size = 10),
        legend.position = "right",                  #设置图例位置
        legend.text = element_text(size = "10"))    #设置图例字体大小

ggRadar(data = Idata, aes(group = Species), alpha = 0)+
  theme(axis.text = element_text(size = 10),
        legend.position = "right",                  #设置图例位置
        legend.text = element_text(size = "10"))    #设置图例字体大小

IDdata<-Idata
IDdata[,5]<-1:150

ggRadar(data = IDdata, aes(group = Species), alpha = 0)+
  theme(axis.text = element_text(size = 10),
        legend.position = "right",                  #设置图例位置
        legend.text = element_text(size = "10"))    #设置图例字体大小

ggRadar(data = IDdata[1:50,], aes(group = Species), alpha = 0)

ggRadar(data = IDdata[51:100,], aes(group = Species), alpha = 0)

ggRadar(data = IDdata[101:150,], aes(group = Species), alpha = 0)

气泡图

library(DescTools)
## Warning: 程辑包'DescTools'是用R版本4.2.2 来建造的
cols = c("olivedrab1","orange","green","mediumturquoise","mediumorchid2","firebrick1")
gp<-Idata[,5]
PlotBubble(x = Idata[,1], y = Idata[,2], cex=.004,
           area = Idata[,3], col = cols[gp], border="grey50",
           panel.first=grid(), xlab="Sepal.Length", ylab="Sepal.Width", las=1
)


legend(x="bottomright", fill=cols[1:3], legend=levels(gp))

(2) 星图和脸图

###星图

data.iris<- as.matrix(iris[,1:4])
stars(data.iris,draw.segments = T, key.loc = c(20, 0,10))

脸图

library(aplpack)
## Warning: 程辑包'aplpack'是用R版本4.2.2 来建造的
## Registered S3 method overwritten by 'aplpack':
##   method       from     
##   plot.bagplot DescTools
## 
## 载入程辑包:'aplpack'
## The following object is masked from 'package:DescTools':
## 
##     plot.bagplot
faces(data.m.setosa, face.type=1, scale = T)

## effect of variables:
##  modified item       Var           
##  "height of face   " "Sepal.Length"
##  "width of face    " "Sepal.Width" 
##  "structure of face" "Petal.Length"
##  "height of mouth  " "Petal.Width" 
##  "width of mouth   " "Sepal.Length"
##  "smiling          " "Sepal.Width" 
##  "height of eyes   " "Petal.Length"
##  "width of eyes    " "Petal.Width" 
##  "height of hair   " "Sepal.Length"
##  "width of hair   "  "Sepal.Width" 
##  "style of hair   "  "Petal.Length"
##  "height of nose  "  "Petal.Width" 
##  "width of nose   "  "Sepal.Length"
##  "width of ear    "  "Sepal.Width" 
##  "height of ear   "  "Petal.Length"
faces(IDdata, face.type=1, scale = T)

## effect of variables:
##  modified item       Var           
##  "height of face   " "Sepal.Length"
##  "width of face    " "Sepal.Width" 
##  "structure of face" "Petal.Length"
##  "height of mouth  " "Petal.Width" 
##  "width of mouth   " "Species"     
##  "smiling          " "Sepal.Length"
##  "height of eyes   " "Sepal.Width" 
##  "width of eyes    " "Petal.Length"
##  "height of hair   " "Petal.Width" 
##  "width of hair   "  "Species"     
##  "style of hair   "  "Sepal.Length"
##  "height of nose  "  "Sepal.Width" 
##  "width of nose   "  "Petal.Length"
##  "width of ear    "  "Petal.Width" 
##  "height of ear   "  "Species"