-1

I am trying to create a tiled plot using plot.kmeans functions from the package "useful". This can be done very easily for base plot function using par() or layout(), or in ggplot using facests. I want to visualize results of various runs of kmeans with different number of clusters using plot or plot.kmeans function from the package "useful". I have tried par() and layout(), but I only get one plot not multiple plots. Consider the following code segment:

results1 <- kmeans(x=dataset1,centers=5,nstart = 25)
results2 <- kmeans(x=dataset2,centers=5,nstart = 25)
par(mfrow=c(2,1))
plot.kmeans(results1,dataset1)
plot.kmeans(results2,dataset2)

I have two datasets and apply kmeans on them separately. I want to draw results of both datasets side by side. plot.kmeans is good function to see results of clustering. But somehow i feel that we cannot two or more plots side by side like we do in case of base plotting facility. If instead of plot.kmeans, I use base plot functions it will work. So thats my problem in brief. Thanks.

MrGumble
  • 5,631
  • 1
  • 18
  • 33
Aftab
  • 21
  • 5
  • 2
    Could you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – csgroen Nov 09 '17 at 13:23
  • I have added code in question, please have a look again. – Aftab Nov 10 '17 at 10:16

1 Answers1

0

The function plot.kmeans from the package "useful" (not to be confused with general qualifier 'a useful package) returns a ggplot2 object. These do not work with par() or layout().

Look instead at grid.arrange from the "gridExtra" package:

results1 <- kmeans(x=dataset1,centers=5,nstart = 25)
results2 <- kmeans(x=dataset2,centers=5,nstart = 25)
library(gridExtra)
p1 <- plot.kmeans(results1,dataset1)
p2 <- plot.kmeans(results2,dataset2)
grid.arrange(p1, p2, ncol=2)
MrGumble
  • 5,631
  • 1
  • 18
  • 33