# ------------------------------------------------ # Program: Univariate2.R # Author: Steven M. Boker # Date: Tue Sep 18 11:11:24 EDT 2007 # # Provides examples of the use of scatterplots, boxplots and # histograms using R syntax. # # ------------------------------------------------ # ------------------------------------------------ # Create two data vectors and a dataframe of them. X <- c(1,2,3,4,5,6,7,8,9,10) Y <- c(2,4,3,5,4,6,5,7,6,8) Test2 <- data.frame(X=X, Y=Y) Test2 summary(Test2) dim(Test2) # ------------------------------------------------ # Scatter plot. pdf("Univariate2Scatter1.pdf", height=5, width=5) plot(X, Y) dev.off() # ------------------------------------------------ # Scatter plot with fixed axis scales. pdf("Univariate2Scatter2.pdf", height=5, width=5) plot(c(0,10),c(0,10), main = "Test 2", ylab = "Y", xlab = "X", type="n") lines(X, Y, type="p") dev.off() # ------------------------------------------------ # Read the iris data. iris <- data.frame(read.table("iris.dat", header=T)) summary(iris) dim(iris) # ------------------------------------------------ # Box plot of Iris sepal length. pdf("Univariate2Boxplot1.pdf", height=5, width=6) boxplot(iris$sepal.length, ylab="sepal.length", col="light green") dev.off() # ------------------------------------------------ # Multiple boxplots on the same page. pdf("Univariate2Boxplot2.pdf", height=7, width=7) par(mfrow=c(2,2)) boxplot(iris$sepal.length, ylab="sepal.length", col="light green") boxplot(iris$sepal.width, ylab="sepal.width", col="light green") boxplot(iris$petal.length, ylab="petal.length", col="light green") boxplot(iris$petal.width, ylab="petal.width", col="light green") dev.off() # ------------------------------------------------ # Multiple boxplots in the same graph. pdf("Univariate2Boxplot3.pdf", height=5, width=6) boxplot(list(iris$sepal.length, iris$sepal.width, iris$petal.length, iris$petal.width), names=c("sepal.length", "sepal.width", "petal.length", "petal.width"), col=c("red", "blue", "green", "purple")) dev.off() pdf("Univariate2Boxplot3a.pdf", height=5, width=6) boxplot(list(iris$sepal.length, iris$sepal.width, iris$petal.length, iris$petal.width), names=c("sepal.length", "sepal.width", "petal.length", "petal.width"), col=heat.colors(4)) dev.off() pdf("Univariate2Boxplot3b.pdf", height=5, width=6) boxplot(list(iris$sepal.length, iris$sepal.width, iris$petal.length, iris$petal.width), names=c("sepal.length", "sepal.width", "petal.length", "petal.width"), col=c("skyblue1")) dev.off() colors() # ------------------------------------------------ # Splitting on a variable to create multiple boxplots. pdf("Univariate2Boxplot4.pdf", height=5, width=6) boxplot(split(iris$sepal.length, as.factor(iris$variety)), ylab="sepal.length") dev.off() # ------------------------------------------------ # Putting it all together. pdf("Univariate2Boxplot5.pdf", height=7, width=7) par(mfrow=c(2,2)) boxplot(split(iris$sepal.length, as.factor(iris$variety)), main="a", ylab="sepal.length") boxplot(split(iris$sepal.width, as.factor(iris$variety)), main="b", ylab="sepal.width") boxplot(split(iris$petal.length, as.factor(iris$variety)), main="c", ylab="petal.length") boxplot(split(iris$petal.width, as.factor(iris$variety)), main="d", ylab="petal.width") dev.off() # ------------------------------------------------ # ------------------------------------------------ # ------------------------------------------------ # Histogram of sepal length. pdf("Univariate2Histogram1-4.pdf", height=5, width=6) hist(iris$sepal.length, nclass=4, xlab="sepal.length", col="aquamarine", main="") dev.off() pdf("Univariate2Histogram1-8.pdf", height=5, width=6) hist(iris$sepal.length, nclass=8, xlab="sepal.length", col="aquamarine", main="") dev.off() pdf("Univariate2Histogram1-16.pdf", height=5, width=6) hist(iris$sepal.length, nclass=16, xlab="sepal.length", col="aquamarine", main="") dev.off() pdf("Univariate2Histogram1-32.pdf", height=5, width=6) hist(iris$sepal.length, nclass=32, xlab="sepal.length", col="aquamarine", main="") dev.off() pdf("Univariate2Histogram1-64.pdf", height=5, width=6) hist(iris$sepal.length, nclass=64, xlab="sepal.length", col="aquamarine", main="") dev.off() # ------------------------------------------------ # Histogram of sepal length with density curve. pdf("Univariate2HistogramDensity1.pdf", height=5, width=6) tdensity <- density(iris$sepal.length) thist <- hist(iris$sepal.length, nclass=10, xlab="sepal.length", freq=FALSE, main="", col="salmon") lines(tdensity$x, tdensity$y, type='l') dev.off() # ------------------------------------------------ # Multiple histograms with density curves. pdf("Univariate2HistogramDensity2.pdf", height=8, width=8) par(mfrow=c(2,2)) tdensity <- density(iris$sepal.length) thist <- hist(iris$sepal.length, nclass=10, xlab="sepal.length", freq=FALSE, main="a", col="seagreen1") lines(tdensity$x, tdensity$y, type='l') tdensity <- density(iris$sepal.width) thist <- hist(iris$sepal.width, nclass=10, xlab="sepal.width", freq=FALSE, main="b", col="seagreen1") lines(tdensity$x, tdensity$y, type='l') tdensity <- density(iris$petal.length) thist <- hist(iris$petal.length, nclass=10, xlab="petal.length", freq=FALSE, main="c", col="seagreen1") lines(tdensity$x, tdensity$y, type='l') tdensity <- density(iris$petal.width) thist <- hist(iris$petal.width, nclass=10, xlab="petal.width", freq=FALSE, main="d", col="seagreen1") lines(tdensity$x, tdensity$y, type='l') dev.off()