# NOT RUN {
# 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())
# }
Run the code above in your browser using DataLab