################################################### ### chunk number 2: callEjemplo1 ################################################### # postscript("Ejemplo1.eps") # Para llamar al dispositivo postscript opt<-par(bg="lightyellow",mfrow=c(1,1)) # Ajustes previos barplot(VADeaths, beside = T, # col = c("lightblue", "mistyrose", "lightcyan", "lavender", "cornsilk"), col = c("blue", "pink", "cyan", "yellow", "red"), legend = rownames(VADeaths), ylim = c(0, 100)) title(main = "Death Rates in Virginia", font.main = 3) # Ajustes posteriores par(opt) # dev.off () # Para cerrar el dispositivo ################################################### ### chunk number 3: ejemplo1called ################################################### # postscript("Ejemplo1.eps") # Para llamar al dispositivo postscript opt<-par(bg="lightyellow",mfrow=c(1,1)) # Ajustes previos barplot(VADeaths, beside = T, # col = c("lightblue", "mistyrose", "lightcyan", "lavender", "cornsilk"), col = c("blue", "pink", "cyan", "yellow", "red"), legend = rownames(VADeaths), ylim = c(0, 100)) title(main = "Death Rates in Virginia", font.main = 3) # Ajustes posteriores par(opt) # dev.off () # Para cerrar el dispositivo ################################################### ### chunk number 5: plot1fig ################################################### library(DAAG) attach(elasticband) oldpar <- par(cex.main=1.5, mex=1.5, bg="lightblue") plot(distance~stretch) par(oldpar) # Restores the earlier settings detach(elasticband);detach(package:DAAG) ################################################### ### chunk number 6: plotFigure1 ################################################### library(DAAG) attach(elasticband) oldpar <- par(cex.main=1.5, mex=1.5, bg="lightblue") plot(distance~stretch) par(oldpar) # Restores the earlier settings detach(elasticband);detach(package:DAAG) ################################################### ### chunk number 8: plot2fig ################################################### library(DAAG) attach(elasticband) oldpar <- par(cex.main=1.5, mex=1.5) plot(distance~stretch,main="Analisis de elasticidad", xlab="Estiramiento", ylab="Distancia", sub ="Tomado de 'Using R...'", xlim=c(40,60), ylim=c(100,200)) par(oldpar) # Restores the earlier settings detach(elasticband);detach(package:DAAG) ################################################### ### chunk number 9: plotFigure2 ################################################### library(DAAG) attach(elasticband) oldpar <- par(cex.main=1.5, mex=1.5) plot(distance~stretch,main="Analisis de elasticidad", xlab="Estiramiento", ylab="Distancia", sub ="Tomado de 'Using R...'", xlim=c(40,60), ylim=c(100,200)) par(oldpar) # Restores the earlier settings detach(elasticband);detach(package:DAAG) ################################################### ### chunk number 11: plot3fig ################################################### library(DAAG) attach(elasticband) oldpar <- par(cex=1.5, mex=1.5) plot(distance~stretch,main="Analisis de elasticidad", xlab="Estiramiento", ylab="Distancia") abline(lm(distance~stretch)) text(stretch,distance, rownames(elasticband),pos=2,cex=0.8,col="red") par(oldpar) # Restores the earlier settings detach(elasticband);detach(package:DAAG) ################################################### ### chunk number 12: plotFigure3 ################################################### library(DAAG) attach(elasticband) oldpar <- par(cex=1.5, mex=1.5) plot(distance~stretch,main="Analisis de elasticidad", xlab="Estiramiento", ylab="Distancia") abline(lm(distance~stretch)) text(stretch,distance, rownames(elasticband),pos=2,cex=0.8,col="red") par(oldpar) # Restores the earlier settings detach(elasticband);detach(package:DAAG) ################################################### ### chunk number 14: plot4fig ################################################### # 3.3.1 Size, colour and choice of plotting symbol plot(1, 1, xlim=c(1, 7.5), ylim=c(0,5), type="n") # Do not plot points points(1:7, rep(4.5, 7), cex=1:7, col=1:7, pch=0:6) text(1:7,rep(3.5, 7), labels=paste(0:6), cex=1:7, col=1:7) points(1:7,rep(2,7), pch=(0:6)+7) # Plot symbols 7 to 13 text((1:7)+0.25, rep(2,7), paste((0:6)+7)) # Label with symbol number points(1:7,rep(1,7), pch=(0:6)+14) # Plot symbols 14 to 20 text((1:7)+0.25, rep(1,7), paste((0:6)+14)) # Labels with symbol number ################################################### ### chunk number 15: CreaplotFigure5 ################################################### # 3.3.1 Size, colour and choice of plotting symbol plot(1, 1, xlim=c(1, 7.5), ylim=c(0,5), type="n") # Do not plot points points(1:7, rep(4.5, 7), cex=1:7, col=1:7, pch=0:6) text(1:7,rep(3.5, 7), labels=paste(0:6), cex=1:7, col=1:7) points(1:7,rep(2,7), pch=(0:6)+7) # Plot symbols 7 to 13 text((1:7)+0.25, rep(2,7), paste((0:6)+7)) # Label with symbol number points(1:7,rep(1,7), pch=(0:6)+14) # Plot symbols 14 to 20 text((1:7)+0.25, rep(1,7), paste((0:6)+14)) # Labels with symbol number ################################################### ### chunk number 17: plot6fig ################################################### library(MASS);data(hills) pairs(hills) detach(package:MASS) ################################################### ### chunk number 18: plotFigure6 ################################################### library(MASS);data(hills) pairs(hills) detach(package:MASS) ################################################### ### chunk number 20: plot7fig ################################################### 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 <- cor(x, y,use="complete.obs") txt <- format(c(r, 0.123456789), digits=digits)[1] txt <- paste(prefix, txt, sep="") if(missing(cex.cor)) cex <- 0.8/strwidth(txt) text(0.5, 0.5, txt, cex = cex * 0.5) } # pdf(file.path(resultsDir,"CorrelationPlots.raw.pdf")) pairs(hills,main="Correlation between genes (alls spots)", #upper.panel=panel.smooth, lower.panel=panel.cor) #dev.off() ################################################### ### chunk number 21: plotFigure7 ################################################### 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 <- cor(x, y,use="complete.obs") txt <- format(c(r, 0.123456789), digits=digits)[1] txt <- paste(prefix, txt, sep="") if(missing(cex.cor)) cex <- 0.8/strwidth(txt) text(0.5, 0.5, txt, cex = cex * 0.5) } # pdf(file.path(resultsDir,"CorrelationPlots.raw.pdf")) pairs(hills,main="Correlation between genes (alls spots)", #upper.panel=panel.smooth, lower.panel=panel.cor) #dev.off() ################################################### ### chunk number 23: plot8fig ################################################### library(MASS);attach(Animals) par(mfrow=c(2,2), pch=16) plot(body, brain) plot(sqrt(body), sqrt(brain)) plot((body)^0.1, (brain)^0.1) plot(log(body),log(brain)) par(mfrow=c(1,1), pch=1) mtext("Relación entre el peso del cuerpo y del cerebro",line=2) detach(Animals);detach(package:MASS) ################################################### ### chunk number 24: plotFigure8 ################################################### library(MASS);attach(Animals) par(mfrow=c(2,2), pch=16) plot(body, brain) plot(sqrt(body), sqrt(brain)) plot((body)^0.1, (brain)^0.1) plot(log(body),log(brain)) par(mfrow=c(1,1), pch=1) mtext("Relación entre el peso del cuerpo y del cerebro",line=2) detach(Animals);detach(package:MASS) ################################################### ### chunk number 25: Murrell-StdPlots-1 ################################################### # # Comment: # # Examples of the use of standard high-level plotting functions. # # In each case, extra output is also added using low-level # plotting functions. # oldpar<-par(mfrow=c(3, 2)) # Scatterplot x <- c(0.5, 2, 4, 8, 12, 16) y1 <- c(1, 1.3, 1.9, 3.4, 3.9, 4.8) y2 <- c(4, .8, .5, .45, .4, .3) par(las=1, mar=c(4, 4, 2, 4)) plot.new() plot.window(range(x), c(0, 6)) lines(x, y1) lines(x, y2) points(x, y1, pch=16, cex=2) points(x, y2, pch=21, bg="white", cex=2) par(col="grey50", fg="grey50", col.axis="grey50") axis(1, at=seq(0, 16, 4)) axis(2, at=seq(0, 6, 2)) axis(4, at=seq(0, 6, 2)) box(bty="u") mtext("Travel Time (s)", side=1, line=2, cex=0.8) mtext("Responses per Travel", side=2, line=2, las=0, cex=0.8) mtext("Responses per Second", side=4, line=2, las=0, cex=0.8) text(4, 5, "Bird 131") par(mar=c(5.1, 4.1, 4.1, 2.1), col="black", fg="black", col.axis="black") # Histogram # Random data Y <- rnorm(50) # Make sure no Y exceed [-3.5, 3.5] Y[Y < -3.5 | Y > 3.5] <- NA x <- seq(-3.5, 3.5, .1) dn <- dnorm(x) par(mar=c(4.5, 4.1, 3.1, 0)) hist(Y, breaks=seq(-3.5, 3.5), ylim=c(0, 0.5), col="grey80", freq=FALSE) lines(x, dnorm(x), lwd=2) par(mar=c(5.1, 4.1, 4.1, 2.1)) # Barplot # Modified from example(barplot) par(mar=c(2, 3.1, 2, 2.1)) midpts <- barplot(VADeaths, col=grey(0.5 + 1:5/12), names=rep("", 4)) mtext(sub(" ", "\n", colnames(VADeaths)), at=midpts, side=1, line=0.5, cex=0.5) text(rep(midpts, each=5), apply(VADeaths, 2, cumsum) - VADeaths/2, VADeaths, col=rep(c("white", "black"), times=2:3, cex=0.8)) par(mar=c(5.1, 4.1, 4.1, 2.1)) # Boxplot # Modified example(boxplot) - itself from suggestion by Roger Bivand par(mar=c(3, 4.1, 2, 0)) boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1:3 - 0.2, subset= supp == "VC", col="grey90", xlab="", ylab="tooth length", ylim=c(0,35)) mtext("Vitamin C dose (mg)", side=1, line=2.5, cex=0.8) boxplot(len ~ dose, data = ToothGrowth, add = TRUE, boxwex = 0.25, at = 1:3 + 0.2, subset= supp == "OJ", col="grey70") legend(1.5, 9, c("Ascorbic acid", "Orange juice"), bty="n", fill = c("grey90", "grey70")) par(mar=c(5.1, 4.1, 4.1, 2.1)) # Persp # Almost exactly example(persp) x <- seq(-10, 10, length= 30) y <- x f <- function(x,y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r } z <- outer(x, y, f) z[is.na(z)] <- 1 # 0.5 to include z axis label par(mar=c(0, 0.5, 0, 0), lwd=0.1) persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "grey80") par(mar=c(5.1, 4.1, 4.1, 2.1), lwd=1) # Piechart # Example 4 from help(pie) par(mar=c(0, 2, 1, 2), xpd=FALSE, cex=0.5) pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12) names(pie.sales) <- c("Blueberry", "Cherry", "Apple", "Boston Cream", "Other", "Vanilla") pie(pie.sales, col = gray(seq(0.4,1.0,length=6))) par<-oldpar ################################################### ### chunk number 26: Trellis1 ################################################### library(MASS);library(lattice) print(histogram( ~ CW | sp, data = crabs)) detach(package:MASS);detach(package:lattice) ################################################### ### chunk number 27: Trellis2 ################################################### library(MASS);library(lattice) print(bwplot( ~ CW | sp, data = crabs, layout = c(1, 2))) detach(package:MASS);detach(package:lattice) ################################################### ### chunk number 28: Trellis3 ################################################### library(MASS);library(lattice) print(bwplot(sex ~ CW | sp, data = crabs, layout = c(1, 2))) detach(package:MASS);detach(package:lattice)