Reputation: 123
I am using VSCode and I am trying to visualize a dendrogram produced by hclust(). My data set (baseMini) is made of 2d vectors and so far this is what I have done:
dd <- dist(x = baseMini, method = "euclidean")
hc <- hclust(d = dd, method = "complete")
plot_hc <- plot(x = hc)
I confirmed that dd is a distance matrix, it looks something like this:
1 2 3 3000 3001 3002
2 1.6710624
3 0.9892719 2.0635176
3000 6.4243003 7.9459624 5.9004292
3001 6.3986419 7.9904130 5.9925833 0.8342891
3002 7.6690587 8.9921632 6.9431019 2.1140049 2.8991615
6001 7.5003784 5.9874636 7.3277637 12.6488917 12.9759681 12.8943846
Then, trying to check hc I get something like this (I really don't know what to expect as an hclust() output):
> hc
Call:
hclust(d = dd, method = "complete")
Cluster method : complete
Distance : euclidean
Number of objects: 7
However, when I want to show the plot with show(plot_hc) or even just ask for plot_hc I get a NULL output. Everywhere I looked for they always use these same steps and never mention any particular library or anything else. What am I missing and why can't VSCode/R recognize the plot?
Additional info: I tried asking with plot.hclust(hc) but I get that there is no function "plot.hclust". Another thing that could be is that at the beginning of the file I used the library(ggplot) and I don't know if it interferes somehow with plot(). Also, maybe the error is in how I prepared my data set, but it looks pretty standard:
> baseMini
X1 X2
1 2.1135390 0.3644160
2 0.4427359 0.3938498
3 2.2764659 1.3401791
3000 7.8718230 3.2128892
3001 8.1685351 2.4331455
3002 7.9622546 5.3249591
6001 -4.7765455 3.3279293
Any help is highly appreciated.
Upvotes: 0
Views: 146
Reputation: 3236
This stumped me also. It's not an issue with it being an hclust
object but rather how the plot()
function works. ggplot()
outputs save nicely to your environment but it doesn't work the same for plot()
. I saw a few approaches that recommend using recordPlot()
. This answer can provide more detail and another approach. Good luck.
# these steps don't work
doesnt_work <- plot(1:3, 1:3)
doesnt_work
#> NULL
# but these steps do
plot(1:3, 1:3) # make plot
p <- recordPlot() # assign existing plot outputoutput
plot.new() # clean up device
p # redraw
Upvotes: 2