iris <- read.csv("iris.csv")Module 06 Exercise
Module 6 Exercise Using Module 3
This report generates the information from Module 3, Part 1. Module 3 Part 1 includes several graphs comparing Petal Length to Petal Width for the data found in iris.csv.
Reading in the Data
First, we must read in the data from iris.csv
Petal Length vs. Petal Width
Our first graph shows the relationship between Petal Length (x) and Petal Width (y) of these irises.
plot(iris$Petal.Length, iris$Petal.Width, xlab = "Petal Length (cm)", ylab = "Petal Width (cm)",
main = "Petal Length vs. Petal Width")Petal Length and Width for Setosa Irises
Next we want to see the relationship for setosa irises specifically.
iris.setosa <- subset(iris, iris$Species == "setosa")
range(iris.setosa$Petal.Width)[1] 0.1 0.6
plot(iris.setosa$Petal.Length, iris.setosa$Petal.Width, xlab = "Petal Length (cm)",
ylab = "Petal Width (cm)", main = "Setosa Petal Length vs. Petal Width")Separated by Species with Symbols
Now we look at how the species of iris is related to the petal length and width.
table(iris$Species)
setosa versicolor virginica
50 50 50
iris$SpeciesNum <- iris$Species
iris$SpeciesNum [iris$Species == "setosa"] = 1
iris$SpeciesNum [iris$Species == "versicolor"] = 2
iris$SpeciesNum [iris$Species == "virginica"] = 3
iris$SpeciesNum = as.numeric(iris$SpeciesNum)
iris$SpeciesNum [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[38] 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[75] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3
[112] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[149] 3 3
plot(iris$Petal.Length, iris$Petal.Width, xlab = "Petal Length (cm)", ylab = "Petal Width (cm)",
main = "Petal Length vs. Petal Width", pch = iris$SpeciesNum)
legend("topleft", pch = 1:3, legend = c("setosa", "versicolor", "virginica"), title = "Species")Separated by Species with Colors
Now we use color to show how the species of iris is related to the petal length and width.
iris$SpeciesCol <- iris$Species
iris$SpeciesCol [iris$Species == "setosa"] = "darkorange"
iris$SpeciesCol [iris$Species == "versicolor"] = "lightblue"
iris$SpeciesCol [iris$Species == "virginica"] = "pink"
plot(iris$Petal.Length, iris$Petal.Width, xlab = "Petal Length (cm)", ylab = "Petal Width (cm)",
main = "Petal Length vs. Petal Width", pch = 16, col = iris$SpeciesCol)
legend("topleft", pch = 16, legend = c("setosa", "versicolor", "virginica"),
col = c('darkorange', 'lightblue', 'pink'), title = "Species")Sepal Width and Species Petal Length and Width
This plot shows the Petal Length (x) and Petal Width (y) with symbols scaled by the value of the Sepal Width and colored by species.
range(iris$Sepal.Width)[1] 2.0 4.4
iris$Sepal.Width.Scale <- iris$Sepal.Width
iris$Sepal.Width.Scale [iris$Sepal.Width < 3.0] = 16
iris$Sepal.Width.Scale [iris$Sepal.Width >= 3.0 & iris$Sepal.Width < 4.0] = 17
iris$Sepal.Width.Scale [iris$Sepal.Width >= 4.0] = 18
iris$Sepal.Width.Scale [1] 17 17 17 17 17 17 17 17 16 17 17 17 17 17 18 18 17 17 17 17 17 17 17 17 17
[26] 17 17 17 17 17 17 17 18 18 17 17 17 17 17 17 17 16 17 17 17 17 17 17 17 17
[51] 17 17 17 16 16 16 17 16 16 16 16 17 16 16 16 17 17 16 16 16 17 16 16 16 16
[76] 17 16 17 16 16 16 16 16 16 17 17 17 16 17 16 16 17 16 16 16 17 16 16 16 16
[101] 17 16 17 16 17 17 16 16 16 17 17 16 17 16 16 17 17 17 16 16 17 16 16 16 17
[126] 17 16 17 16 17 16 17 16 16 16 17 17 17 17 17 17 17 16 17 17 17 16 17 17 17
plot(iris$Petal.Length, iris$Petal.Width, xlab = "Petal Length (cm)", ylab = "Petal Width (cm)",
main = "Petal Length vs. Petal Width", pch = iris$Sepal.Width.Scale, col = iris$SpeciesCol)
legend("topleft", legend = c("setosa", "versicolor", "virginica"),
fill = c('darkorange', 'lightblue', 'pink'), title = "Species")
legend("bottomright", legend = c("2.0 to 2.99", "3.0 to 3.99", "4.0+"),
pch = c(16, 17, 18), title = "Sepal Width (cm)")Smoothing Line for Petal Length (x) and Petal Width (y)
This smoothing line shows the approximate relationship for Petal Length and Petal Width.
scatter.smooth(iris$Petal.Length, iris$Petal.Width, xlab = "Petal Length (cm)",
ylab = "Petal Width (cm)", main = "Petal Length vs. Petal Width")