8

Today I have realised that the silhouette plot in the cluster package doesn't display properly in RStudio. A Google search revealed that someone else had had a problem with this:

http://support.rstudio.org/help/discussions/problems/3094-plotsnot-showing-up-in-r-studio

Being new to R, it was unclear to me whether the problem had been resolved in this thread! So my question is: is there a way to get the silhouette plot to display properly in RStudio?

Thanks for any help.

Example script:

library(cluster)
data(xclara)
km <- kmeans(xclara,3)
dissE <- daisy(xclara)
sk <- silhouette(km$cl, dissE)
plot(sk)
www
  • 38,575
  • 12
  • 48
  • 84
user32259
  • 1,113
  • 3
  • 13
  • 21

2 Answers2

11

Seems like the thread you reference was pretty explicit: the silhouette package may have a bug wrt png output, and RStudio doesn't play nice with some other graphics formats. So you need to specify, as Josh wrote, "The pdf(), quartz(), and windows() devices..." when using RStudio.

Edit: so what you need to do is

pdf('my_nice_plot.pdf')
plot(sk)
dev.off()

Which writes your plot directly to the file. You might try replacing the first line with png('my_nice_plot.png') and so on, as those should work as well. But I doubt you'll get a clean plot in RStudio's graphics window until they upgrade their interface.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • Thanks. As mentioned in the initial question, as I am new to `R` (or not very clever), I couldn't work out whether the thread had concluded with a solution. If there is code that I can add to the example script (see above) would you be so kind as to tell me what it is? Cheers. – user32259 Mar 04 '13 at 13:19
  • 1
    How to use it with shiny? I am getting a blank plot when using it with shiny – Vipin Verma Apr 21 '17 at 02:50
  • @vipin8169 Sorry, I've never used `shiny` – Carl Witthoft Apr 21 '17 at 12:13
  • @vipin8169 I managed to get it work in shiny by first exporting the silhouette plot to a temp pdf file (`pdf(tempfile()) ... dev.off()`). I then convert this file into a temp png file with `animation::im.convert()` and finally rendering the png with `shiny::renderImage()` This is quite twisted but it works. Hope this can help. – majpark Apr 07 '20 at 15:38
1

This may be just an issue of visibility of the actual plot: try

library(cluster)
data(xclara)
km <- kmeans(xclara[1:100,],3)
dissE <- daisy(xclara[1:100,])
sk <- silhouette(km$cl, dissE)
plot(sk)
JMV
  • 19
  • 1