First we need to read in our iris data and plot Length (x) and Width (y)
iris = read.csv("iris.csv")
plot(iris$Petal.Length, iris$Petal.Width, main = "Petal Length Vs. Petal Width",
xlab = "Petal Length", ylab = "Petal Width")
We can also create plots for a single species, Setosa for example. We need to create first create a factor for the species code and then create a subset of data with only the species of interest. Then we can plot our newly created subset.
SpeciesS = factor(iris$Code)
setosaIris = subset(iris, SpeciesS == 1)
plot(setosaIris$Petal.Length, setosaIris$Petal.Width,
main = "Petal Length Vs. Petal Width (Setosa)",
xlab = "Petal Length", ylab = "Petal Width")
We can also plot each species using its own unique symbol, and add a legend so we can understand which is which.
plot(iris$Petal.Length, iris$Petal.Width, main = "Petal Length Vs. Petal Width",
xlab = "Petal Length", ylab = "Petal Width", pch = iris$Code)
legend("topleft", pch = 1:3, legend = c("Setosa", "Versicolor", "Virginica"),
ncol = 1)
We can assign a color to each species, and then create a plot with each species it’s own unique color.
iris$color = iris$Code
iris$color [iris$Code == 1] = "orange"
iris$color [iris$Code == 2] = "purple"
iris$color [iris$Code == 3] = "pink"
plot(iris$Petal.Length, iris$Petal.Width, main = "Petal Length Vs. Petal Width",
xlab = "Petal Length", ylab = "Petal Width", col = iris$color, pch = 16)
legend("topleft", pch = 16, legend = c("Setosa", "Versicolor", "Virginica"),
ncol = 1, col = unique(iris$color))
We can also scale the size of each of our data points depending on some other data value. We will scale the size of each point in our plot by the corresponding flower’s Sepal Width
plot(iris$Petal.Length, iris$Petal.Width, main = "Petal Length Vs. Petal Width",
xlab = "Petal Length", ylab = "Petal Width", col = iris$color, pch = 16,
cex = (iris$Sepal.Width)/2.5)
legend("topleft", pch = 16, legend = c("Setosa", "Versicolor", "Virginica"),
ncol = 1, col = unique(iris$color))
One last thing we can do to our plot is add a smoothing line, which can help us visualize a trend in the data.
scatter.smooth(iris$Petal.Length, iris$Petal.Width, main = "Petal Length Vs. Petal Width",
xlab = "Petal Length", ylab = "Petal Width", col = iris$color, pch = 16, span = 0.45)
legend("topleft", pch = 16, legend = c("Setosa", "Versicolor", "Virginica"),
ncol = 1, col = unique(iris$color))