1 多組圖git
setwd("E:/R/R-beginer-guide/data/RBook") Benthic <- read.table(file="RIKZ2.txt",header = TRUE) pairs(Benthic[,2:9])
效果:shell
上圖每一個圖形都重複了一遍ide
解決:函數
pairs(Benthic[,2:9],diag.panel=panel.hist, upper.panel=panel.smooth, lower.panel=panel.cor)
在R控制檯中執行報錯:ui
+ lower.panel=panel.cor) 錯誤於pairs.default(Benthic[, 2:9], diag.panel = panel.list, upper.panel = panel.smooth, : 找不到對象'panel.cor'
解決這個錯誤spa
手下在控制檯敲入:rest
?pairs
彈出的頁面中能夠找到兩函數orm
第一個對象
第二個blog
panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...) { usr <- par("usr"); on.exit(par(usr)) par(usr = c(0, 1, 0, 1)) r <- abs(cor(x, y)) txt <- format(c(r, 0.123456789), digits=digits)[1] txt <- paste(prefix, txt, sep="") if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt) text(0.5, 0.5, txt, cex = cex.cor * r) }
錯誤心中報的是
找不到對象'panel.cor'
所以在R控制檯執行第二個函數
而後在執行pair函數就可獲得正確的效果圖
2 協同圖
實戰1
coplot(Richness ~ NAP | as.factor(Beach),pch=19,data=Benthic)
效果:
實戰2
coplot(Richness ~ NAP | grainsize,pch=19,data=Benthic)
效果略
實戰3
panel.lm=function(x,y,...) { tmp <- lm(y ~ x,na.action = na.omit) abline(tmp) points(x,y,...) } coplot(Richness ~ NAP |as.factor(Beach), pch=19,panel=panel.lm,data = Benthic)
實戰4 兩個條件變量的協同圖
pHEire <- read.table(file="SDI2003.txt",header = TRUE) pHEire$LOGAlt <- log10(pHEire$Altitude) pHEire$fForested <- factor(pHEire$Forested) coplot(pH ~ SDI | LOGAlt * fForested, panel=panel.lm,data=pHEire)
實戰5
pHEire$Temp2 <- cut(pHEire$Temperature,breaks=2) pHEire$Temp2.num <- as.numeric(pHEire$Temp2) coplot(pH ~ SDI | LOGAlt*fForested, panel=panel.lm,data = pHEire, number = 3,cex=1.5,pch=19, col = gray(pHEire$Temp2.num/3))
3 組合不一樣類型的圖
MyLayOut <- matrix(c(2,0,1,3),nrow=2,ncol=2,byrow=TRUE) MyLayOut nf <- layout(mat = MyLayOut,widths = c(3,1), heights = c(1,3),respect = TRUE) show(nf) xrange <- c(min(Benthic$NAP),max(Benthic$NAP)) yrange <- c(min(Benthic$Richness),max(Benthic$Richness)) #First graph par(mar = c(4,4,2,2)) plot(Benthic$NAP,Benthic$Richness , frame.plot = FALSE,xlim = xrange,ylim = yrange,ylab = "Richness") #Second graph par(mar = c(0,3,1,1)) boxplot(Benthic$NAP,horizontal=TRUE,axes = FALSE, frame.plot = FALSE,ylim= xrange,space = 0) #Third graph par(mar=c(3,0,1,1)) boxplot(Benthic$Richness,axes = FALSE,ylim = yrange,space = 0,horiz= TRUE)