## ----------------------------------------------------------------------------- data(cars) head(cars) ## ----plotcars1, fig.cap=''---------------------------------------------------- plot(cars) ## ----plotcars2, fig.cap=''---------------------------------------------------- plot(cars[,1], cars[,2]) ## ----plotcars3, fig.cap=''---------------------------------------------------- plot(cars[,1], cars[,2], xlab='Speed of car (miles/hr)', ylab='Stopping distance (feet)') ## ----plotcars4, fig.cap=''---------------------------------------------------- plot(cars, xlab='Speed of car (miles/hr)', ylab='Stopping distances (feet)', pch=20, cex=2, col='red') ## ----plotcars5, fig.cap=''---------------------------------------------------- plot(cars, xlab='Speed', ylab='Time', pch=20, cex=2, col='red', xlim = c(0,25), las=1) ## ----plotcars6, fig.cap=''---------------------------------------------------- plot(cars, xlab='Speed', ylab='', pch=20, cex=2, col='red', xlim = c(0,27), ylim=c(0,125), axes=FALSE, xaxs="i", yaxs="i") axis(1) axis(2, las=1) text(5, 100, 'Cars!', cex=2, col='blue') par(xpd=NA) text(-1, 133, 'Distance\n(feet)') ## ----plotcars6b, fig.cap=''--------------------------------------------------- set.seed(0) brands <- c('Buick', 'Chevrolet', 'Ford') b <- sample(brands, nrow(cars), replace=TRUE) i <- match(b, brands) plot(cars, pch=i+1, cex=i, col=rainbow(3)[i]) j <- 1:length(brands) legend(5, 120, brands, pch=(j+1), pt.cex=j, col=rainbow(3), cex=1.5) ## ----plotcars7, fig.cap=''---------------------------------------------------- par(mfrow=c(2,2), mar=c(2,3,1.5,1.5)) for (i in 1:4) { plot(sample(cars[,1]), sample(cars[,2]), xlab='', ylab='', las=1) } ## ----othplot1----------------------------------------------------------------- head(InsectSprays) ## ----othplot2, fig.cap=''----------------------------------------------------- hist(InsectSprays[,1]) ## ----othplot3, fig.cap=''----------------------------------------------------- x <- aggregate(InsectSprays[,1,drop=F], InsectSprays[,2,drop=F], sum) barplot(x[,2], names=x[,1], horiz=T, las=1) ## ----othplot4, fig.cap=''----------------------------------------------------- boxplot(count ~ spray, data = InsectSprays, col = "lightgray") ## ----------------------------------------------------------------------------- data(iris) str(iris) ## ----iris1, fig.width = 4, fig.height = 4------------------------------------- plot(iris$Sepal.Length, type = 'l') plot(iris$Sepal.Length, type = 'p') plot(iris$Sepal.Length, type = 'o') plot(iris$Sepal.Length, iris$Sepal.Width, type = 'p') ## ----iris2, fig.width = 4, fig.height = 4------------------------------------- plot(iris$Petal.Length, iris$Petal.Width, main = "Edgar Anderson's Iris Data", xlab = "Petal Length", ylab = "Petal Width") ## ----iris3, fig.width = 4, fig.height = 4------------------------------------- plot(iris$Sepal.Length, type = 'l', lty = 2, lwd = 2, main = "Edgar Anderson's Iris Data", xlab = "Petal Length", ylab = "Petal Width") plot(iris$Petal.Length, iris$Petal.Width, pch = 8, main = "Edgar Anderson's Iris Data", xlab = "Petal Length", ylab = "Petal Width") ## ----iris4, fig.width = 4, fig.height = 4------------------------------------- plot(iris$Petal.Length, iris$Petal.Width, pch = 8, col = 'red', main = "Edgar Anderson's Iris Data", xlab = "Petal Length", ylab = "Petal Width") ## ----iris5, fig.width = 4, fig.height = 4------------------------------------- mycolor <- c("red","green3","blue")[as.factor(iris$Species)] plot(iris$Petal.Length, iris$Petal.Width, pch = 8, col = mycolor, main = "Edgar Anderson's Iris Data", xlab = "Petal Length", ylab = "Petal Width") ## ----iris6, fig.width = 4, fig.height = 4------------------------------------- plot(iris$Petal.Length, iris$Petal.Width, pch = 8, col = mycolor, main = "Edgar Anderson's Iris Data", xlab = "Petal Length", ylab = "Petal Width", xlim = c(0,10), ylim= c(0,10)) ## ----iris7, fig.width = 4, fig.height = 4------------------------------------- plot(iris$Petal.Length, iris$Petal.Width, pch = 8, col = mycolor, main = "Edgar Anderson's Iris Data", xlab = "Petal Length", ylab = "Petal Width", xlim = c(0,10), ylim= c(0,10), axes = FALSE) axis(1, at = 1:10, lab = c(1:10)) axis(2, at = 1:10, lab = c(1:10)) ## ----iris10, fig.width = 4, fig.height = 4------------------------------------ plot(iris$Petal.Length, iris$Petal.Width, pch = 8, col = mycolor, main = "Edgar Anderson's Iris Data", xlab = "Petal Length", ylab = "Petal Width", xlim = c(0,10), ylim= c(0,10)) legend('topright', legend = unique(iris$Species), col = c("red","green3","blue"), pch = 8) ## ----iris11, fig.width = 4, fig.height = 4------------------------------------ loc <- list() loc$x <- 2.75 loc$y <- 4.94 plot(iris$Petal.Length, iris$Petal.Width, pch = 8, col = mycolor, main = "Edgar Anderson's Iris Data", xlab = "Petal Length", ylab = "Petal Width", xlim = c(0,10), ylim= c(0,10)) legend('topright', legend = unique(iris$Species), col = c("red","green3","blue"), pch = 8, bty = 'n') text(loc$x, loc$y, labels = "Hello! this is a Scatter Plot") ## ----iris12, fig.width = 8, fig.height = 8------------------------------------ par(mfrow=c(2,2)) boxplot(iris$Sepal.Length~iris$Species, main = "Boxplot of Sepal Length", xlab = "Species", ylab = "Sepal Length", col = c("red","green3","blue"), cex.lab = 1.25) hist(iris$Sepal.Length, main = "Histogram of Sepal Length", xlab = "Sepal Length", ylab = "Frequency", col = c("grey"), cex.lab = 1.25) plot(iris$Sepal.Length, type = 'l', lty = 2, lwd = 2, col = 'red', main = "Variation with of Petal Width and Petal Length", xlab = "Petal Length", ylab = "Petal Width", cex.lab = 1.25) plot(iris$Petal.Length, iris$Petal.Width, pch = 8, col = mycolor, main = "Scatter plot of Petal Width and Petal Length", xlab = "Petal Length", ylab = "Petal Width", cex.lab = 1.25) ## ----pdf, eval = FALSE-------------------------------------------------------- ## pdf("theplot.pdf") ## boxplot(iris$Sepal.Length~Species, main = "Boxplot of Sepal Length", ## xlab = "Species", ylab = "Sepal Length", col = c("red","green3","blue")) ## dev.off() ## ----png, eval = FALSE-------------------------------------------------------- ## png(filename = "myplot.png", width = 200, height = 300, units = "cm", res = 300) ## ## boxplot(iris$Sepal.Length~iris$Species, main = "Boxplot of Sepal Length", ## xlab = "Species", ylab = "Sepal Length", col = c("red","green3","blue")) ## ## dev.off()