Skip to content

Instantly share code, notes, and snippets.

@ottadini
Last active January 3, 2016 04:59
Show Gist options
  • Save ottadini/8413205 to your computer and use it in GitHub Desktop.
Save ottadini/8413205 to your computer and use it in GitHub Desktop.
Function to plot a segments plot copied and modified from kohonen package.
# Copying the kohonen:::plot.kohcodes routine, for equivalent kmeans stars diagram
plot.centers <- function (x, main)
{
palette.name <- terrain.colors
codes <- x$codes
nvars <- ncol(codes)
maxlegendcols <- 3
codeRendering <- "segments"
margins <- rep(0.6, 4)
if (!is.null(main))
margins[3] <- margins[3] + 2
par(mar = margins)
if (codeRendering == "segments" & nvars < 15 & !is.null(colnames(codes))) {
plot(x$grid, ylim = c(max(x$grid$pts[, 2]) + min(x$grid$pts[, 2]), -2))
current.plot <- par("mfg") # only needed when multiple plots
plot.width <- diff(par("usr")[1:2])
cex <- 1
leg.result <- legend(x = mean(x$grid$pts[, 1]), xjust = 0.5,
y = 0, yjust = 1, legend = colnames(codes), cex = cex,
plot = FALSE, ncol = min(maxlegendcols, nvars),
fill = palette.name(nvars))
while (leg.result$rect$w > plot.width) { # is it too wide?
cex <- cex * 0.9
leg.result <- legend(x = mean(x$grid$pts[, 1]),
xjust = 0.5, y = 0, yjust = 1, legend = colnames(codes),
cex = cex, plot = FALSE, ncol = min(maxlegendcols, nvars),
fill = palette.name(nvars))
}
leg.result <- legend(x = mean(x$grid$pts[, 1]), xjust = 0.5,
y = 0, yjust = 1, cex = cex, legend = colnames(codes),
plot = FALSE, ncol = min(maxlegendcols, nvars),
fill = palette.name(nvars))
par(mfg = current.plot)
plot(x$grid, ylim = c(max(x$grid$pts[, 2]) + min(x$grid$pts[, 2]), -leg.result$rect$h))
legend(x = mean(x$grid$pts[, 1]), xjust = 0.5, y = 0,
yjust = 1, cex = cex, plot = TRUE, legend = colnames(codes),
ncol = min(maxlegendcols, nvars), fill = palette.name(nvars))
}
else {
plot(x$grid)
}
title.y <- max(x$grid$pts[, 2]) + 1.2
if (title.y > par("usr")[4] - 0.2) {
title(main)
}
else {
text(mean(range(x$grid$pts[, 1])), title.y, main,
adj = 0.5, cex = par("cex.main"), font = par("font.main"))
}
bgcol <- "transparent"
symbols(x$grid$pts[, 1], x$grid$pts[, 2],
circles = rep(0.5, nrow(x$grid$pts)), inches = FALSE, add = TRUE, bg = bgcol)
codemins <- apply(codes, 2, min)
codes <- sweep(codes, 2, codemins)
switch(codeRendering, segments = {
stars(codes, locations = x$grid$pts, labels = NULL,
len = 0.4, add = TRUE, col.segments = palette.name(nvars),
draw.segments = TRUE)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment