# R in action读书笔记（15）第十一章 中级绘图 之二 折线图 相关图 马赛克图

plot

legend

corrgram

mosaic

11.2折线图

`> opar<-par(no.readonly=TRUE)> par(mfrow=c(1,2))> t1<-subset(Orange,Tree==1)> plot(t1\$age,t1\$circumference,xlab="Age(days)",ylab="circumference (mm)",main="orange tree 1growth")> plot(t1\$age,t1\$circumference,xlab="Age(days)",ylab="circumference (mm)",main="orange tree 1growth",type="b")> par(opar)`

p

l

o

b、c

s、S

h

n

`> opar<-par(no.readonly=TRUE)> par(mfrow=c(2,4))> t1<-subset(Orange,Tree==1)> plot(t1\$age,t1\$circumference,xlab="Age(days)",ylab="circumference(mm)",main="type=p",type="p")> plot(t1\$age,t1\$circumference,xlab="Age(days)",ylab="circumference(mm)",main="type=l",type="l")> plot(t1\$age,t1\$circumference,xlab="Age(days)",ylab="circumference(mm)",main="type=o",type="o")> plot(t1\$age,t1\$circumference,xlab="Age(days)",ylab="circumference(mm)",main="type=b",type="b")> plot(t1\$age,t1\$circumference,xlab="Age(days)",ylab="circumference(mm)",main="type=c",type="c")> plot(t1\$age,t1\$circumference,xlab="Age(days)",ylab="circumference(mm)",main="type=s",type="s")> plot(t1\$age,t1\$circumference,xlab="Age(days)",ylab="circumference(mm)",main="type=S",type="S")> plot(t1\$age,t1\$circumference,xlab="Age(days)",ylab="circumference(mm)",main="type=h",type="h")`

`> 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 lineplot")> legend(xrange[1],yrange[2], 1:ntrees, cex=.8, pch=plotchar, lty=linetype, title="tree")`

11.3 相关图

`> library(corrgram)> corrgram(mtcars,order=TRUE,lower.panel=panel.shade,upper.panel=panel.pie,text.panel=panel.txt, main="correlogram of mtcarsintercorrelations")`

corrgram()函数的格式如下：

`corrgram(x, type=NULL, order = FALSE, labels,panel=panel.shade,lower.panel=panel,upper.panel=panel,diag.panel=NULL,text.panel=textPanel,label.pos=c(0.5, 0.5), label.srt=0, cex.labels=NULL,font.labels=1,row1attop=TRUE, dir="",gap=0,abs=FALSE,col.regions=colorRampPalette(c("red","salmon","white","royalblue","navy")),cor.method="pearson", ...)`

`> library(corrgram)> corrgram(mtcars,order=TRUE,lower.panel=panel.ellipse,upper.panel=panelNaNs,text.panel=panel.txt,lwd=1.5,diag.panel=panel.minmax,main="correlogram of mtcars using\nscatter plots and ellipse")`

mtcars数据框中变量的相关系数图。下三角区域包含平滑拟合曲线和置信椭圆，上三角区域包含散点图。主对角面板包含变量最小和最大值。矩阵的行和列利用主成分分析法进行了重排序

`> library(corrgram)> corrgram(mtcars,lower.panel=panel.shade,upper.panel=NULL,text.panel=panel.txt, lwd=1.5, main="Car mileage data(unsorted)")`

11.4 马赛克图

`> ftable(Titanic)Survived No YesClass Sex Age1st Male Child 0 5Adult 118 57FemaleChild 0 1Adult 4 1402nd Male Child 0 11Adult 154 14FemaleChild 0 13Adult 13 803rd Male Child 35 13Adult 387 75FemaleChild 17 14Adult 89 76Crew Male Child 0 0Adult 670 192FemaleChild 0 0Adult 3 20`

mosaic()函数可按如下方式调用

mosaic(table)

`> library(vcd)> mosaic(Titanic,shade=TRUE,legend=TRUE)`

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