### R code from vignette source 'Chapter02_Fundamentals.prnw' ################################################### ### code chunk number 1: initialize ################################################### # chunk to silently load all needed packages and common datasets library("compositions") # Datafile geochemsed.csv missing in SVN!!!!! sed+++ GeoChemSed=read.csv("geochemsed.csv", header=TRUE, skip=1)[,-c(3,14,31,32)] #GeoChemSed=read.csv("GraVel.csv",header=TRUE)[,7:10] ################################################### ### code chunk number 2: Chapter02_Fundamentals.prnw:68-70 ################################################### library(compositions) kitchen <- read.table("SimpleData.txt",sep="") ################################################### ### code chunk number 3: Chapter02_Fundamentals.prnw:102-103 ################################################### (coi <- clo(kitchen,parts=c("Fat","Carbonates","Protein"),total=100)) ################################################### ### code chunk number 4: Chapter02_Fundamentals.prnw:160-162 ################################################### amounts <- kitchen[,c("Fat","Carbonates","Protein")] amounts ################################################### ### code chunk number 5: Chapter02_Fundamentals.prnw:165-166 ################################################### (sumPresent <- totals(rplus(amounts))) ################################################### ### code chunk number 6: Chapter02_Fundamentals.prnw:175-178 ################################################### Other <- kitchen[,"total"] - sumPresent Amounts <- cbind(amounts,Other=Other) # rplus? clo(Amounts) ################################################### ### code chunk number 7: Chapter02_Fundamentals.prnw:219-222 ################################################### Amounts[4,]*100/82 Amounts[4,] acomp(Amounts[4,]) ################################################### ### code chunk number 8: Chapter02_Fundamentals.prnw:242-243 ################################################### (epm <- acomp(c(Fat=37,Protein=17,Carbonates=17))) ################################################### ### code chunk number 9: Chapter02_Fundamentals.prnw:249-250 ################################################### ec <- acomp(coi)+epm ################################################### ### code chunk number 10: perturbe ################################################### (ec <- acomp(coi)+epm) ################################################### ### code chunk number 11: Chapter02_Fundamentals.prnw:326-327 (eval = FALSE) ################################################### ## fatDataset$total.Fat = totals(aplus(fatDataset,c("sat.Fat","unsat.Fat")))#$ ################################################### ### code chunk number 12: Chapter02_Fundamentals.prnw:588-590 ################################################### GeoChemSed=read.csv("geochemsed.csv", header=TRUE, skip=1)[,-c(3,14,31,32)] names(GeoChemSed) ################################################### ### code chunk number 13: Harker-example ################################################### par(mfrow=c(1,2), mar=c(4,4,0.5,0.5)) plot( clo(GeoChemSed[,3:12])[,c(4,5)] ) plot( clo(GeoChemSed[,c(4,6,7,11,12)])[,c(2,3)] ) ################################################### ### code chunk number 14: Chapter02_Fundamentals.prnw:654-656 (eval = FALSE) ################################################### ## xc = acomp(GeoChemSed, c("MgO","CaO","Na2O")) ## plot(xc) ################################################### ### code chunk number 15: FirstTernary ################################################### opar <- par(mfrow=c(1,2), mar=c(3,3,1,1)) x <- acomp(GeoChemSed[7:9]) nx <- colnames(x) pmat <- persp(0:1,0:1,outer(0:1,0:1,function(x,y) pmax(1-x-y,0)),xlab=nx[1],ylab=nx[2],zlab=nx[3]) #,main="perspective plot\n of subcomposition") polygon(trans3d(c(1,0,0),c(0,1,0),c(0,0,1),pmat),col="white") points(trans3d(x[,1],x[,2],x[,3],pmat)) arrowPoints <- trans3d(c(0.95,0.85,0.6),c(0.95,0.85,0.6),c(0.95,0.85,0.6),pmat) arrows(arrowPoints$x[1],arrowPoints$y[1],arrowPoints$x[3],arrowPoints$y[3]) text(arrowPoints$x[2],arrowPoints$y[2],"viewing direction",pos=2) plot(x) #title("ternary diagram") par(opar) ################################################### ### code chunk number 16: logratios-example ################################################### par(mar=c(4,4,1,1)) plot(log(TiO2/K2O)~log(CaO/P2O5), GeoChemSed) ################################################### ### code chunk number 17: Chapter02_Fundamentals.prnw:751-753 (eval = FALSE) ################################################### ## plot(log(var1/var2)~log(var3/var4), dataset) ## plot(I(var1/var2)~I(var3/var4), dataset, log="xy") ################################################### ### code chunk number 18: BarPie-example ################################################### layout(matrix(c(1,2),ncol=2), width=c(1.5,1)) par(mar=c(3,3,1,1)) barplot( acomp(Amounts),xlim=c(0,11) ) pie( acomp(Amounts["wheat",]), main="wheat") ################################################### ### code chunk number 19: Chapter02_Fundamentals.prnw:793-795 ################################################### barplot( acomp(Amounts),xlim=c(0,11) ) pie( acomp(Amounts["wheat",]), main="wheat") ################################################### ### code chunk number 20: Chapter02_Fundamentals.prnw:928-930 ################################################### data(quakes) MultivariateData <- rmult(quakes[,c("lat","long","depth")]) ################################################### ### code chunk number 21: Chapter02_Fundamentals.prnw:964-966 ################################################### data(phosphor) x <- rplus(phosphor,c("inorg","organic")) ################################################### ### code chunk number 22: Chapter02_Fundamentals.prnw:991-993 ################################################### data(iris) x <- aplus(iris[,-1]) ################################################### ### code chunk number 23: Chapter02_Fundamentals.prnw:1249-1252 ################################################### data(Hydrochem) dat <- acomp(Hydrochem[,6:19]) class(dat) ################################################### ### code chunk number 24: perturbeBeispiel1 ################################################### (x = acomp(c(1,2,3))) (y = acomp(c(1,2,1))) (z = x+y) ################################################### ### code chunk number 25: perturbeBeispiel2 (eval = FALSE) ################################################### ## data(Hydrochem) ## Xmas = Hydrochem[,c("Cl","HCO3","SO4")] ## Xmas = acomp(Xmas) ## mw = c(35.453, 61.017, 96.063) ## mw = acomp(mw) ## Xmol = Xmas-mw ################################################### ### code chunk number 26: Chapter02_Fundamentals.prnw:1391-1392 ################################################### 2*y ################################################### ### code chunk number 27: Chapter02_Fundamentals.prnw:1425-1426 (eval = FALSE) ################################################### ## acos(scalar(normalize(x),normalize(y))) ################################################### ### code chunk number 28: Chapter02_Fundamentals.prnw:1454-1462 ################################################### a = acomp(c(1,2,4)) b = acomp(c(3,3,1)) mycomp = rbind(a,b) mycomp = acomp(mycomp) dist(mycomp) norm(a-b) dist(mycomp,method="manhattan") sum(abs(clr(a-b))) ################################################### ### code chunk number 29: Chapter02_Fundamentals.prnw:1592-1598 ################################################### a = c(1,2,4) (ac = acomp(a)) ilr(ac) clr2ilr(clr(ac)) (Vd = ilrBase(x=a)) clr(ac) %*% Vd