#Setup setwd("C:/Users/Mark.Williamson.2/Desktop/Willliamson Presentations/Statistical Modules") #you'll have to set your own library(ggplot2) library(dplyr) library(rgl) ############################################################################################# #Bubble Plots #iris head(iris) ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width, size=Petal.Length))+ geom_point(alpha=0.7) ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width, size=Petal.Length, color=Species))+ geom_point(alpha=0.7) #insular insular<-read.csv("insular.csv") attach(insular) print(insular) ggplot(data=insular, aes(x=Area ,y=elevation, size=SpeciesRichness)) + geom_point(alpha=0.7) ############################################################################################# #3D Scatterplots #iris par(mar=c(0,0,0,0)) plot3d( x=iris$Sepal.Length, y=iris$Sepal.Width, z=iris$Petal.Length) # Add a new column with color mycolors <- c('royalblue1', 'darkcyan', 'oldlace') iris$color <- mycolors[ as.numeric(iris$Species)] plot3d( x=iris$Sepal.Length, y=iris$Sepal.Width, z=iris$Petal.Length, col = iris$color, type = 's', radius = .1, xlab="Sepal Length", ylab="Sepal Width", zlab="Petal Length") #insular plot3d( x=Area, y=elevation, z=SpeciesRichness, col=c("red"), type='s', size=2) par(mar=c(5.1, 4.1, 4.1, 2.1)) ############################################################################################# #PCA #iris # log transform log.ir <- log(iris[, 1:4]) ir.species <- iris[, 5] # apply PCA - scale. = TRUE is highly # advisable, but default is FALSE. ir.pca <- prcomp(log.ir, center = TRUE, scale. = TRUE) print(ir.pca) plot(ir.pca, type = "l") summary(ir.pca) library(ggfortify) autoplot(ir.pca) autoplot(ir.pca, data = iris, colour = 'Species', loadings=TRUE, loadings.label=TRUE, loadings.label.size=3) #insular head(insular) in.islands<-insular[, 1] in.pca <- prcomp(insular[,2:12], center = TRUE, scale. = TRUE) print(in.pca) plot(in.pca, type = "l") summary(in.pca) autoplot(in.pca) autoplot(in.pca, data=insular, colour='Islands', loadings=TRUE, loadings.label=TRUE, loadings.label.size=3) ############################################################################################# #Scatterplot matrices: # Basic Scatterplot Matrix pairs(~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width, data=iris) pairs(~SpeciesRichness+endemic+Area+altitude+elevation+distanceParamo, data=insular) #Multiplots: Scars <-read.csv("SAS cars.csv") head(Scars) attach(Scars) boxplot(MSRP) boxplot(MSRP~DriveTrain) boxplot(MSRP~DriveTrain*Origin, col=c('blue','red','green')) boxplot(MSRP~DriveTrain*Origin*Type, col=c('blue','red','green'), cex.axis=0.5) par(mfrow=c(3,2)) boxplot(MSRP[Type=='Hybrid']~DriveTrain[Type=='Hybrid']*Origin[Type=='Hybrid'], main='Hybrid',cex.axis=0.75) boxplot(MSRP[Type=='SUV']~DriveTrain[Type=='SUV']*Origin[Type=='SUV'], main='SUV',cex.axis=0.75) boxplot(MSRP[Type=='Sedan']~DriveTrain[Type=='Sedan']*Origin[Type=='Sedan'], main='Sedan',cex.axis=0.75) boxplot(MSRP[Type=='Sports']~DriveTrain[Type=='Sports']*Origin[Type=='Sports'], main='Sports',cex.axis=0.75) boxplot(MSRP[Type=='Truck']~DriveTrain[Type=='Truck']*Origin[Type=='Truck'], main='Truck',cex.axis=0.75) boxplot(MSRP[Type=='Wagon']~DriveTrain[Type=='Wagon']*Origin[Type=='Wagon'], main='Wagon',cex.axis=0.75) par(mfrow=c(1,1)) #Monstrosity boxplot(MSRP~DriveTrain*Origin*Type*as.factor(Cylinders)) #Scatter Plot Colored by categories lnMSRP <-log(MSRP) plot(MPG_Highway, lnMSRP, pch=16, cex=0.70) plot(MPG_Highway, lnMSRP, pch=16, cex=0.70, col=Origin) legend("topright", legend=c("Asia","Europe","US"), fill=c('black','green','red'))