################################################################
#
# Plot GA output function
# Xu, Weijie; Chen, Yuwen; Adams, Cameron; Zhou, Yilin
#
# Final Projct
# STAT 243
# Fall 2017
#
################################################################
#' Plotting function for GA object
#'
#' Takes as input from \code{\link{select}} and creates a plot of fitness for all generations and lines plots of mean and max fitness across generations.
#'
#' @param x a GA object as generated by \code{\link{select}}. Must be of class GA.
#' @param ... a
#' @param col1 a character string for color of chromosomes scatter plotpoints. Default is blue.
#' @param col2 a character string for color of chromosomes mean and max lines. Default is red.
#'
#' @export
plot.GA <- function(x, ..., col1 = "blue", col2 = "red") {
# error checking
if (!is.character(col1)) stop("Error: col1 must be a character string of length 1")
if (length(col1) > 1) stop("Error: col1 must be a character string of length 1")
if (!is.character(col2)) stop("Error: col1 must be a character string of length 1")
if (length(col2) > 1) stop("Error: col1 must be a character string of length 1")
# get plot data from GA object
convergeData <- x$convergeData
obj_fun <- as.character(x$optimize[1])
minimize <- x$optimize[3]
method <- as.character(x$optimize[4])
iter <- x$iter
# scatter plot of chromosome data across generations ----------------
# check for scales packages
if (requireNamespace("scales", quietly = TRUE)) {
# with scales
graphics::plot(jitter(rep(1, nrow(convergeData))), convergeData[, 2, 1], type = "p",
pch = 19, col = scales::alpha(col1, 0.1),
ylim = c(min(convergeData[ , 2, ], na.rm = T),
max(convergeData[ , 2, ], na.rm = T)),
xlim = c(1, iter), xlab = "Generations", ylab = obj_fun,
main = paste("GA performance: \n ", obj_fun, method))
for (i in 2:iter) {
graphics::points(jitter(rep(i, nrow(convergeData))),
convergeData[, 2, i], type = "p",
pch = 19, col = scales::alpha(col1, 0.25))
}
} else {
# without scales
graphics::plot(jitter(rep(1, nrow(convergeData))), convergeData[, 2, 1], type = "p",
pch = 19, col = col1,
ylim = c(min(convergeData[ , 2, ], na.rm = T),
max(convergeData[ , 2, ], na.rm = T)),
xlim = c(1, iter), xlab = "Generations", ylab = obj_fun,
main = paste("GA performance: \n ", obj_fun, method))
for (i in 2:iter) {
graphics::points(jitter(rep(i, nrow(convergeData))),
convergeData[, 2, i], type = "p",
pch = 19, col = "blue")
}
}
# line plots of mean and max fitness per generation
graphics::lines(1:iter, sapply(1:iter,
function(x) convergeData[1, 2, x]), type = "l",
col = col2, lwd = 2)
graphics::lines(1:iter, sapply(1:iter,
function(x) mean(convergeData[, 2, x])),
col = col2, lty = 2, lwd = 2)
# legend
if (minimize == TRUE) {location <- "topright"
} else {location <- "bottomright"}
graphics::legend(location, c("Chr fitness", "Best fitness", "Mean Fitness"),
col = c(col1, col2, col2), pch = c(19, NA, NA), lwd = c(NA, 2, 2),
lty = c(NA, 1, 2), bty = "n")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.