# plot.ga-method: Plot of Genetic Algorithm search path In GA: Genetic Algorithms

## Description

The `plot` method for `ga-class` objects gives a plot of best and average fitness values found during the iterations of the GA search.

## Usage

 ```1 2 3 4``` ```## S4 method for signature 'ga' plot(x, y, ylim, cex.points = 0.7, col = c("green3", "dodgerblue3", adjustcolor("green3", alpha.f = 0.1)), pch = c(16, 1), lty = c(1,2), legend = TRUE, grid = graphics:::grid, ...) ```

## Arguments

 `x` An object of class `"ga"`. `y` Not used. `ylim` A vector of two values specifying the limits on the y-axis. `cex.points` The magnification to be used for points. `col` The colours to be used for best and average fitness values. `pch` The type of points to be used for best and average fitness values. `lty` The type of lines to be used for best and average fitness values. `legend` A logical specifying if a legend should be included. `grid` A function for grid drawing of NULL to avoid drawing one. `...` Further arguments, currently not used.

## Details

Plot best and average fitness values at each iteration of GA search.

## Value

The method invisibly return a `data.frame` with the iterations and summary statistics for the fitness function evaluated at each iteration.

## Author(s)

Luca Scrucca

`ga`, `ga-class`.

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35``` ```# See examples in help(ga) # The following code shows how to obtain graphs using the # ggplot2 plotting system ## Not run: GA <- ga(type = "real-valued", fitness = function(x) -(abs(x)+cos(x)), lower = -20, upper = 20, popSize = 20, pmutation = 0.2, maxiter = 50) out <- plot(GA) library(reshape2) df <- melt(out[,c(1:3,5)], id.var = "iter") library(ggplot2) ggplot(out) + geom_ribbon(aes(x = iter, ymin = median, ymax = max, colour = "median", fill = "median")) + geom_line(aes(x = iter, y = max, colour = "max")) + geom_point(aes(x = iter, y = max, colour = "max")) + geom_line(aes(x = iter, y = mean, colour = "mean"), lty = 2) + geom_point(aes(x = iter, y = mean, colour = "mean"), pch = 1) + xlab("Generation") + ylab("Fitness values") + scale_colour_manual(breaks = c("max", "mean", "median"), values = c("green3", "dodgerblue3", adjustcolor("green3", alpha.f = 0.1))) + scale_fill_manual(breaks = "median", values = adjustcolor("green3", alpha.f = 0.1)) + guides(fill = "none", colour = guide_legend(override.aes = list(fill = c(NA, NA, adjustcolor("green3", alpha.f = 0.1)), pch = c(19,1,NA)))) + theme_bw() + theme(legend.title = element_blank(), legend.pos = "top", legend.background = element_blank()) ## End(Not run) ```

### Example output

```Loading required package: foreach