This document explains MDS (Multidimensional Scaling) plotting using ggplot2 and ggfortify.
First, install ggfortify from CRAN.
install.packages('ggfortify')
ggfortify let ggplot2 know how to interpret some popular R classes. Even though MDS functions returns matrix or list (not specific class), ggfortify can infer background class from list attribute and perform autoplot.
NOTE Inference from matrix is not supported.
NOTE ggfortify can plot stats::dist instance as heatmap.
library(ggfortify)
autoplot(eurodist)
stats::cmdscale performs Classical MDS and returns point coodinates as matrix, thus you can not use autoplot in this case. However, either eig = TRUE, add = True or x.ret = True is specified, stats::cmdscale return list instead of matrix. In these cases, ggfortify can infer how to plot it via autoplot. Refer to help(cmdscale) to check what these options are.
autoplot(cmdscale(eurodist, eig = TRUE))
Specify label = TRUE to plot labels.
autoplot(cmdscale(eurodist, eig = TRUE), label = TRUE, label.size = 3)
MASS::isoMDS and MASS::sammon perform Non-metric MDS and return list which contains point coordinates. Thus, autoplot can be used.
NOTE On background, autoplot.matrix is called to plot MDS. See help(autoplot.matrix) to check available options.
library(MASS)
autoplot(isoMDS(eurodist), colour = 'orange', size = 4, shape = 3)
## initial value 7.505733
## final value 7.505688
## converged
Passing shape = FALSE makes plot without points. In this case, label is turned on unless otherwise specified.
autoplot(sammon(eurodist), shape = FALSE, label.colour = 'blue', label.size = 3)
## Initial stress : 0.01705
## stress after 10 iters: 0.00951, magic = 0.500
## stress after 20 iters: 0.00941, magic = 0.500