Answers.Body: <p>I ended up using the following code to solve this issue:</p>
  <p>Change the <code>tsv</code> file and the <code>tree</code> file name accordingly. Be mindful of the format the <code>tsv</code> file should be in (indicated in the code).</p>
  <p>Also change the tree and the font scales as needed.</p>
  <pre><code># Load required libraries
  library(ape)
  library(ggtree)
  
  # Read the TSV file with species and node names and numbers
  # Make sure the TSV file has headers: &quot;name&quot;, and &quot;number&quot;
  data &lt;- read.table(&quot;counts.tsv&quot;, header = TRUE, sep = &quot;\t&quot;, fill = TRUE, colClasses = &quot;character&quot;)
  
  # Read the Newick file
  newick_file &lt;- &quot;species_tree.newick&quot;
  tree &lt;- read.tree(newick_file)
  
  # Add labels and numbers to the tree
  add_labels &lt;- function(tree, data) {
    missing_numbers &lt;- c()
    
    for (i in 1:nrow(data)) {
      label_name &lt;- data<span class="math-container">$name[i]
  label_number &lt;- data$</span>number[i]
      if (label_name %in% tree<span class="math-container">$tip.label) {
    tip_index &lt;- match(label_name, tree$tip.label)
    tree$tip.label[tip_index] &lt;- paste(label_name, label_number, sep = ":")
  } else if (label_name %in% tree$</span>node.label) {
        node_index &lt;- match(label_name, tree<span class="math-container">$node.label)
    tree$</span>node.label[node_index] &lt;- paste(label_name, label_number, sep = &quot;:&quot;)
      } else {
        missing_numbers &lt;- c(missing_numbers, label_name)
      }
    }
    
    return(list(tree = tree, missing_numbers = missing_numbers))
  }
  
  # Modify the tree object to include labels and numbers
  result &lt;- add_labels(tree, data)
  tree &lt;- result<span class="math-container">$tree
  missing_numbers &lt;- result$</span>missing_numbers
  
  # Write missing labels to a log file
  if (length(missing_numbers) &gt; 0) {
    writeLines(c(&quot;Missing numbers for the following labels:&quot;, missing_numbers), &quot;missing_labels.log&quot;)
  }
  
  # Convert factors to characters
  tree<span class="math-container">$tip.label &lt;- as.character(tree$</span>tip.label)
  tree<span class="math-container">$node.label &lt;- as.character(tree$</span>node.label)
  
  # Visualize the tree
  p &lt;- ggtree(tree, layout = &quot;rectangular&quot;) +
    geom_tiplab(size = 3) +
    geom_nodelab(size = 3, na.rm = TRUE, nudge_x = 0.008) +
    theme_tree2() +
    theme(legend.position = &quot;none&quot;)
  
  # Save the tree as a PDF
  ggsave(&quot;species_tree_with_numbers.pdf&quot;, p, width = 20, height = 20, dpi = 300)
  <span class="math-container">```</span>
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-17T21:52:22.600Z
Answers.LastActivityDate: 2023-03-17T21:52:22.600Z
Answers.UserDisplayName: Sudoh
Body: <p>This is really a programming question but <code>ggtree</code> might be too obscure for this question to be posted on StackOverFlow as a general R related question.</p>
  <p>I am having trouble extrapolating this this <a href="https://guangchuangyu.github.io/treedata-workshop/r-ggtree-data.html" rel="nofollow noreferrer">tutorial</a> and the <code>%&lt;+%</code> operator in <code>ggtree</code>.</p>
  <p>I have a Newick file and a table that has a number for every tip and node in the Newick file. I want to produce a final graphic that shows a tree where the numbers from the table are shown next to respective tips and nodes.</p>
  <p>What I want to do is close to the tutorial, I want to take a tree object and use a data-frame where one column is node-names and the second column holds numerical information per node. I want to attach the information from the data-frame to the tree object.</p>
  <p>The data-frame looks something like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Node / tips</th>
  <th style="text-align: center;">Count</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">12</td>
  </tr>
  <tr>
  <td style="text-align: left;">B</td>
  <td style="text-align: center;">14</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>So, I want to ultimately display the number 12 next to node A and number 14 next to node B while keeping the names intact. Now I have the code working up to the following point:</p>
  <p>(I am really only just working my way through learning R's grammar of graphics and R in general, so assume I am doing something wrong.)</p>
  <pre><code>library(ggplot2)
  library(ggtree)
  
  x &lt;- read.tree(&quot;tree.txt&quot;)
  info &lt;- read.csv(&quot;counts.csv&quot;,sep = '\t',header=TRUE)
  
  p &lt;- ggtree(x) %&lt;+% info + xlim(-.1, 5)
  
  p2 &lt;- p + geom_tiplab(offset = 0.2) + geom_tippoint(aes(caption = Increase)) + theme_tree2()+geom_tiplab(align=TRUE, linesize=.5)+ xlim(0,25)
  
  ggsave(&quot;test_2.pdf&quot;, width = 60, height = 80, units = &quot;cm&quot;, limitsize = FALSE)
  
  print(p2)
  
  ?geom_tippoint::aes() # is there a way to list all the option inside geom_tippoint::aes()
  </code></pre>
  <p>The above code overlays the <code>Newick_label</code> column with the node names instead of attaching the value in the second column.</p>
  <p>But specifically, I am at a loss as to how I can list the options available within <code>geom_tippoint(aes())</code>. I can use <code>?geom_tippoint</code> to access the documentation of <code>geom_tippoint</code> but I think my task needs to manipulate options within the <code>aes()</code> function and the the geom_tippoint() documentation does not list all the <code>aes()</code> options available. And am stuck trying different keywords for <code>geom_tippoint(aes())</code>.</p>
  <p>Can that be done in <code>ggtree</code> or is this specific application of <code>ggtree</code> obscure enough that there is no way to do this and produce a legible graphic? (eg issue: the graphic getting mushy overlays). I have hundred of (not uniform) genes trees so splitting data for each layer (node and tips) might be not the best way.</p>
  <p>The files if anyone wants to try: <a href="https://drive.google.com/file/d/1z7A0Pq50Zh5zqqSNQ8Yc_k9p5MnWX7Lq/view?usp=sharing" rel="nofollow noreferrer">counts</a> and <a href="https://drive.google.com/file/d/1xGs2zwnS7_43AlH2M9zKU2AKtS0hI4rk/view?usp=sharing" rel="nofollow noreferrer">tree</a></p>
  <p>Clarification on the labels for nodes and tips, I want to label both if possible. The data contains information for both.</p>
  <p>Is annotating tips AND nodes out of the ordinary? Tips and nodes are technically different but I have quantitative data at both levels and am looking for the option to annotate at both levels.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-12-27T17:55:11.983Z
Comments.Text: The answer is `geom_tiplab() ` if I get the chance this week I will post a response on a hypothetical data set, but can't do this today. Oh wait, maybe not, there is still the `tip` versus `node` issue - those are different solutions - might be easier just to copy part of the tree file to see what 'A' and 'B' are. At present the table is called `node/tips`: a tip is a subset of the nodes.
Comments.UserDisplayName: M__
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-12-27T18:16:00.057Z
Comments.Text: I will look into `geom_tiplab()` for now, thanks!
Comments.UserDisplayName: Sudoh
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-12-27T23:14:08.173Z
Comments.Text: There's nothing 'wonky' with any procedure, it's just I don't quite know what you are looking for.  **Main point**, geom_tiplab() only works on tips and the tutorial (and the code) you provided is just for tips. It's different code for nodes and the labels are inserted at different places. Naming internal nodes is not straight forward and I'm not sure how you are doing that. It could be you assume what I would call the **tip**, **taxa** or **terminal node** are what you are calling a **node** and my **taxa label** or **tip label** are what you call a **tip**. If that is the case thats easy.
Comments.UserDisplayName: M__
Tags: <r><phylogenetics><ggtree>
Title: Using R `ggtree`, attach extra data next to the nodes on an illustrated tree/newick file
ThreadText: # Using R `ggtree`, attach extra data next to the nodes on an illustrated tree/newick file
  
  (Asked by: username_0 on 2022-12-27)
  
  This is really a programming question but `ggtree` might be too obscure for this question to be posted on StackOverFlow as a general R related question.
  
  I am having trouble extrapolating this this [tutorial](https://guangchuangyu.github.io/treedata-workshop/r-ggtree-data.html) and the `%<+%` operator in `ggtree`.
  
  I have a Newick file and a table that has a number for every tip and node in the Newick file. I want to produce a final graphic that shows a tree where the numbers from the table are shown next to respective tips and nodes.
  
  What I want to do is close to the tutorial, I want to take a tree object and use a data-frame where one column is node-names and the second column holds numerical information per node. I want to attach the information from the data-frame to the tree object.
  
  The data-frame looks something like this:
  
  | Node / tips | Count |
  | --- | --- |
  | A | 12 |
  | B | 14 |
  
  So, I want to ultimately display the number 12 next to node A and number 14 next to node B while keeping the names intact. Now I have the code working up to the following point:
  
  (I am really only just working my way through learning R's grammar of graphics and R in general, so assume I am doing something wrong.)
  
  ```
  library(ggplot2)
  library(ggtree)
  
  x <- read.tree("tree.txt")
  info <- read.csv("counts.csv",sep = '\t',header=TRUE)
  
  p <- ggtree(x) %<+% info + xlim(-.1, 5)
  
  p2 <- p + geom_tiplab(offset = 0.2) + geom_tippoint(aes(caption = Increase)) + theme_tree2()+geom_tiplab(align=TRUE, linesize=.5)+ xlim(0,25)
  
  ggsave("test_2.pdf", width = 60, height = 80, units = "cm", limitsize = FALSE)
  
  print(p2)
  
  ?geom_tippoint::aes() # is there a way to list all the option inside geom_tippoint::aes()
  
  ```
  
  The above code overlays the `Newick_label` column with the node names instead of attaching the value in the second column.
  
  But specifically, I am at a loss as to how I can list the options available within `geom_tippoint(aes())`. I can use `?geom_tippoint` to access the documentation of `geom_tippoint` but I think my task needs to manipulate options within the `aes()` function and the the geom\_tippoint() documentation does not list all the `aes()` options available. And am stuck trying different keywords for `geom_tippoint(aes())`.
  
  Can that be done in `ggtree` or is this specific application of `ggtree` obscure enough that there is no way to do this and produce a legible graphic? (eg issue: the graphic getting mushy overlays). I have hundred of (not uniform) genes trees so splitting data for each layer (node and tips) might be not the best way.
  
  The files if anyone wants to try: [counts](https://drive.google.com/file/d/1z7A0Pq50Zh5zqqSNQ8Yc_k9p5MnWX7Lq/view?usp=sharing) and [tree](https://drive.google.com/file/d/1xGs2zwnS7_43AlH2M9zKU2AKtS0hI4rk/view?usp=sharing)
  
  Clarification on the labels for nodes and tips, I want to label both if possible. The data contains information for both.
  
  Is annotating tips AND nodes out of the ordinary? Tips and nodes are technically different but I have quantitative data at both levels and am looking for the option to annotate at both levels.
  
  **Tags:** r, phylogenetics, ggtree
  
  > **Comments:**
  > **username_1**: The answer is `geom_tiplab() ` if I get the chance this week I will post a response on a hypothetical data set, but can't do this today. Oh wait, maybe not, there is still the `tip` versus `node` issue - those are different solutions - might be easier just to copy part of the tree file to see what 'A' and 'B' are. At present the table is called `node/tips`: a tip is a subset of the nodes.  
  > **username_0**: I will look into `geom_tiplab()` for now, thanks!  
  > **username_1**: There's nothing 'wonky' with any procedure, it's just I don't quite know what you are looking for.  **Main point**, geom_tiplab() only works on tips and the tutorial (and the code) you provided is just for tips. It's different code for nodes and the labels are inserted at different places. Naming internal nodes is not straight forward and I'm not sure how you are doing that. It could be you assume what I would call the **tip**, **taxa** or **terminal node** are what you are calling a **node** and my **taxa label** or **tip label** are what you call a **tip**. If that is the case thats easy.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2023-03-17. Score: 1 | Accepted answer
  
  I ended up using the following code to solve this issue:
  
  Change the `tsv` file and the `tree` file name accordingly. Be mindful of the format the `tsv` file should be in (indicated in the code).
  
  Also change the tree and the font scales as needed.
  
  ```
  # Load required libraries
  library(ape)
  library(ggtree)
  
  # Read the TSV file with species and node names and numbers
  # Make sure the TSV file has headers: "name", and "number"
  data <- read.table("counts.tsv", header = TRUE, sep = "\t", fill = TRUE, colClasses = "character")
  
  # Read the Newick file
  newick_file <- "species_tree.newick"
  tree <- read.tree(newick_file)
  
  # Add labels and numbers to the tree
  add_labels <- function(tree, data) {
    missing_numbers <- c()
    
    for (i in 1:nrow(data)) {
      label_name <- data$name[i]
  label_number <- data$number[i]
      if (label_name %in% tree$tip.label) {
    tip_index <- match(label_name, tree$tip.label)
    tree$tip.label[tip_index] <- paste(label_name, label_number, sep = ":")
  } else if (label_name %in% tree$node.label) {
        node_index <- match(label_name, tree$node.label)
    tree$node.label[node_index] <- paste(label_name, label_number, sep = ":")
      } else {
        missing_numbers <- c(missing_numbers, label_name)
      }
    }
    
    return(list(tree = tree, missing_numbers = missing_numbers))
  }
  
  # Modify the tree object to include labels and numbers
  result <- add_labels(tree, data)
  tree <- result$tree
  missing_numbers <- result$missing_numbers
  
  # Write missing labels to a log file
  if (length(missing_numbers) > 0) {
    writeLines(c("Missing numbers for the following labels:", missing_numbers), "missing_labels.log")
  }
  
  # Convert factors to characters
  tree$tip.label <- as.character(tree$tip.label)
  tree$node.label <- as.character(tree$node.label)
  
  # Visualize the tree
  p <- ggtree(tree, layout = "rectangular") +
    geom_tiplab(size = 3) +
    geom_nodelab(size = 3, na.rm = TRUE, nudge_x = 0.008) +
    theme_tree2() +
    theme(legend.position = "none")
  
  # Save the tree as a PDF
  ggsave("species_tree_with_numbers.pdf", p, width = 20, height = 20, dpi = 300)
  ```
  
  ```
  
  
  ---
  
  