library(MASS)
library(ggplot2)
library(dplyr)
iris.lda <- lda(Species ~ ., iris)
# formula call: y ~ x
plot_discrim(iris.lda, Petal.Length ~ Petal.Width)
# add data ellipses
plot_discrim(iris.lda, Petal.Length ~ Petal.Width,
ellipse = TRUE)
# add filled ellipses with transparency
plot_discrim(iris.lda, Petal.Length ~ Petal.Width,
ellipse = TRUE,
ellipse.args = list(geom = "polygon", alpha = 0.2))
# customize ellipse level and line thickness
plot_discrim(iris.lda, Petal.Length ~ Petal.Width,
ellipse = TRUE,
ellipse.args = list(level = 0.95, linewidth = 2))
# without contours
# data ellipses
plot_discrim(iris.lda, Petal.Length ~ Petal.Width,
contour = FALSE)
# specifying `vars` as character names for x, y
plot_discrim(iris.lda, c("Petal.Width", "Petal.Length"))
# Define custom colors and shapes, modify theme() and legend.position
iris.colors <- c("red", "darkgreen", "blue")
iris.pch <- 15:17
plot_discrim(iris.lda, Petal.Length ~ Petal.Width) +
scale_color_manual(values = iris.colors) +
scale_fill_manual(values = iris.colors) +
scale_shape_manual(values = iris.pch) +
theme_bw(base_size = 14) +
theme(legend.position = "inside",
legend.position.inside = c(.8, .25))
# Quadratic discriminant analysis gives quite a different result
iris.qda <- qda(Species ~ ., iris)
plot_discrim(iris.qda, Petal.Length ~ Petal.Width)
# Add class labels, with custom styling
plot_discrim(iris.lda, Petal.Length ~ Petal.Width,
labels = TRUE,
labels.args = list(geom = "label", size = 6, fontface = "bold"))
# Add labels with position adjustments
plot_discrim(iris.lda, Petal.Length ~ Petal.Width,
labels = TRUE,
labels.args = list(nudge_y = 0.1, size = 5))
# Plot in discriminant space
plot_discrim(iris.lda, LD2 ~ LD1)
# Reverse the horizontal axis in discriminant space
plot_discrim(iris.lda, LD2 ~ LD1, rev.axes = c(TRUE, FALSE))
# Control axis limits
plot_discrim(iris.lda, LD2 ~ LD1,
xlim = c(-10, 10), ylim = c(-8, 8))
Run the code above in your browser using DataLab