title: Difficulty comparing dendrograms
body: <p>I have a dataset with 2 different (1 externally run, 1 done myself) clustering solutions. I want to compare them using the <code>tanglegram</code> and <code>entanglement</code> commands in the <code>dendextend</code> package, however I keep having errors regarding labels and I cannot figure out why. To illustrate, I've cooked up a simple example using mtcars:</p>
  
  <pre><code>df1 &lt;- mtcars
  df1$ID &lt;- row.names(mtcars)
  clusts &lt;- 1:3
  
  # simulate two different cluster algorithms as columns containing cluster group
  df1$cl1 &lt;- sample(clusts, nrow(df1), replace = TRUE)
  df1$cl2 &lt;- sample(clusts, nrow(df1), replace = TRUE)
  table(df1$cl1, df1$cl2)
  
  # Make a copy
  df2 = df1
  
  # Use data.tree to convert df's to data.trees
  library(data.tree)
  df1$pathString &lt;- paste("Tree1", df1$cl1, df1$ID, sep = "/")
  df2$pathString &lt;- paste("Tree2", df2$cl2, df2$ID, sep = "/")
  
  node1 &lt;- as.Node(df1)
  node2 &lt;- as.Node(df2)
  
  # Convert to dendrograms and compare using dendextend
  library(dendextend)
  dend1 &lt;- as.dendrogram(node1)
  dend2 &lt;- as.dendrogram(node2)
  
  tanglegram(dend1, dend2)
  entanglement(dend1, dend2)
  </code></pre>
  
  <p>This gives these errors:</p>
  
  <pre><code>&gt; tanglegram(dend1, dend2)
  Error in dend12[[1]] : subscript out of bounds
  In addition: Warning message:
  In intersect_trees(dend1, dend2, warn = TRUE) :
    The two trees had no common labels!
  &gt; entanglement(dend1, dend2)
  Error in match_order_by_labels(dend2, dend1) : 
    labels do not match in both trees.  Please make sure to fix the labels    names!
  (make sure also that the labels of BOTH trees are 'character')
  </code></pre>
  
  <p>I do not understand why these errors are occurring and examining the data structures is not giving me the answer! Any helpful enlightenment would be much appreciated!</p>
  
  <p><strong>EDIT</strong>
  Taking note of @emilliman5 's answer below: I understand that my dendrograms are unresolved - I'm not using hierarchical clustering and so I want to compare un-resolved dendrograms. More - I've adopted some code from this question: <a href="https://stackoverflow.com/questions/2310913/how-do-i-manually-create-a-dendrogram-or-hclust-object-in-r">How do I manually create a dendrogram (or &quot;hclust&quot;) object ? (in R)</a> to build the dendrograms myself - and these will produce a tanglegram despite being unresolved. However this is not a solution as its too hard to generalised to varying parameters (my tree depth/resolution varies and trying to write a function to code trees with varying levels of nesting is a road to insanity!).</p>
  
  <pre><code>tree1 &lt;- list()
  attributes(tree1) &lt;- list(members=nrow(df1), height=3)
  class(tree1) &lt;- "dendrogram"
  
  # Assign leaf names to list
  leaves &lt;- list()
  leaf_height_list &lt;- list()
  for(i in 1:length(clusts)){
      leaves[[i]] &lt;- which(df1$cl1 == (i) )
  }
  for(i in 1:length(clusts)){
      tree1[[i]] &lt;- list()
      attributes(tree1[[i]]) &lt;- list(members=length(which(df1$cl1==i)), height=2, edgetext=i)
      for( j in 1:length(leaves[[i]]) ){
          tree1[[i]][[j]] &lt;- list()
          tree1[[i]][[j]] &lt;- leaves[[i]]
          attributes(tree1[[i]][[j]]) &lt;- list(members = 1, height = 1,
                                         label = as.character(leaves[[i]][j]),
                                         leaf = TRUE)
      }
  }
  plot(tree1, center=TRUE)
  
  tree2 &lt;-list();
  attributes(tree2) &lt;- list(members=nrow(df2), height=3)
  class(tree2) &lt;- "dendrogram"
  
  # Assign leaf names to list
  leaves &lt;- list()
  leaf_height_list &lt;- list()
  for(i in 1:length(clusts)){
      leaves[[i]] &lt;- which(df2$cl2 == (i) )
  }
  for(i in 1:length(clusts)){
      tree2[[i]] &lt;- list()
      attributes(tree2[[i]]) &lt;- list(members=length(which(df2$cl2==i)), height=2, edgetext=i)
      for( j in 1:length(leaves[[i]]) ){
          tree2[[i]][[j]] &lt;- list()
          tree2[[i]][[j]] &lt;- leaves[[i]]
          attributes(tree2[[i]][[j]]) &lt;- list(members = 1, height = 1,
                                          label = as.character(leaves[[i]][j]),
                                          leaf = TRUE)
      }
  }
  plot(tree2, center=TRUE)
  
  tanglegram(tree1, tree2)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/9rBpI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9rBpI.jpg" alt="Ugly tanglegram"></a></p>
  
  <p>Its ugly but its all I want/need.</p>
  
  <p>Trying to figure out why this works, if I peek into the dendrograms:</p>
  
  <pre><code>&gt; str(unclass(tree1[[1]][[1]]))
   atomic [1:12] 1 8 9 10 11 13 16 22 25 27 ...
   - attr(*, "members")= num 1
   - attr(*, "height")= num 1
   - attr(*, "label")= chr "1"
   - attr(*, "leaf")= logi TRUE
  </code></pre>
  
  <p>You notice there is a vector. Peeking into a hclust derived dendrogram we see there is also a vector/atomic:</p>
  
  <pre><code>&gt; str(unclass(as.dendrogram(hclust(dist(df1))))[[1]][[1]])
   atomic [1:1] 31
   - attr(*, "members")= int 1
   - attr(*, "height")= num 0
   - attr(*, "label")= chr "Maserati Bora"
   - attr(*, "leaf")= logi TRUE
  </code></pre>
  
  <p>However, peeking into the data.tree created dendrogram I note there is no vector/atomic:</p>
  
  <pre><code>&gt; str(unclass(dend1[[1]][[1]]))
   list()
   - attr(*, "label")= chr "Mazda RX4"
   - attr(*, "members")= num 1
   - attr(*, "height")= num 0
   - attr(*, "leaf")= logi TRUE
  </code></pre>
  
  <p>Could this missing atomic be causing a problem ?</p>