1

I made a phylogenetic tree plot with the igraph package. The problem is, that my tree is too big to fit into the saved image. I'm using Rstudio and usually saving my plots manually. With smaller trees, I was able to enlarge the plot window of Rstudio to make the plot fit into the image, without the nodes overlapping each other. Unfortunately, this isn't working with my latest plot. I tried to save it with png(), pdf() and jpeg() with different width and height, but it still doesn't fit properly. Either the nodes overlap each other or only a part of the plot is visible in the image. The only solution I found so far was to decrease the label size. But in the end, you can't read anything.

Thank you in advance for your help.

Here is my code:

id <- c("Spirochaetota","Brachyspirae","Brevinematia","Leptospirae","Spirochaetia","sk6","Brachyspirales","Brachyspiraceae","Brachyspira",
      "Brevinematales","bo1","Brevinemataceae","Brevinema","bf2","Leptospirales","Leptonemataceae","Leptonema","lg2","Leptospiraceae","Leptospira",
      "LeptospiraA","LeptospiraB","lg3","lf1","Turneriellales","Turneriellaceae","Turneriella","tf1","Borreliales","Borreliaceae","Borrelia",
      "Borreliella","Sphaerochaetales","Sphaerrochaetaceae","Sphaerochaeta","SphaerochaetaA","sg15","SpirochaetalesA","SpirochaetaceaeA",
      "SpirochaetaA","sf1","SpirochaetalesC","Alkalispirochaetaceae","Alkalispirochaeta","Salinispiraceae","Salinispira","SpirochaetaD","sg1",
      "sf6","SpirochaetalesD","sdf1","SpirochaetalesE","SpirochaetaceaeB","Oceanispirochaeta","SpirochaetaE","SpirochaetaF","SpirochaetaG","sg2",
      "Treponematales","Treponemataceae","Treponema","TreponemaB","TreponemaC","TreponemaD","TreponemaF","tg7","TreponemataceaeB","TreponemaE",
      "TreponemaG","TreponemaH","tg5","tf3","so11")

links <- data.frame(from = c("Spirochaetota","Spirochaetota","Spirochaetota","Spirochaetota","Spirochaetota","Brachyspirae",
                "Brachyspirales","Brachyspiraceae","Brevinematia","Brevinematia","Brevinematales","Brevinemataceae",
                "Brevinematales","Leptospirae","Leptospirales","Leptonemataceae","Leptonemataceae","Leptospirales",
                "Leptospiraceae","Leptospiraceae","Leptospiraceae","Leptospiraceae","Leptospirales","Leptospirae",
                "Turneriellales","Turneriellaceae","Turneriellales","Spirochaetia","Borreliales","Borreliaceae",
                "Borreliaceae","Spirochaetia","Sphaerochaetales","Sphaerrochaetaceae","Sphaerrochaetaceae","Sphaerrochaetaceae",
                "Spirochaetia","SpirochaetalesA","SpirochaetaceaeA","SpirochaetalesA","Spirochaetia","SpirochaetalesC",
                "Alkalispirochaetaceae","SpirochaetalesC","Salinispiraceae","Salinispiraceae","Salinispiraceae","SpirochaetalesC",
                "Spirochaetia","SpirochaetalesD","Spirochaetia","SpirochaetalesE","SpirochaetaceaeB","SpirochaetaceaeB",
                "SpirochaetaceaeB","SpirochaetaceaeB","SpirochaetaceaeB","Spirochaetia","Treponematales","Treponemataceae",
                "Treponemataceae","Treponemataceae","Treponemataceae","Treponemataceae","Treponemataceae","Treponematales",
                "TreponemataceaeB","TreponemataceaeB","TreponemataceaeB","TreponemataceaeB","Treponematales","Spirochaetia"),
                to = c("Brachyspirae","Brevinematia","Leptospirae","Spirochaetia","sk6","Brachyspirales",
                       "Brachyspiraceae","Brachyspira","Brevinematales","bo1","Brevinemataceae","Brevinema",
                       "bf2","Leptospirales","Leptonemataceae","Leptonema","lg2","Leptospiraceae",
                       "Leptospira","LeptospiraA","LeptospiraB","lg3","lf1","Turneriellales",
                       "Turneriellaceae","Turneriella","tf1","Borreliales","Borreliaceae","Borrelia",
                       "Borreliella","Sphaerochaetales","Sphaerrochaetaceae","Sphaerochaeta","SphaerochaetaA","sg15",
                       "SpirochaetalesA","SpirochaetaceaeA","SpirochaetaA","sf1","SpirochaetalesC","Alkalispirochaetaceae",
                       "Alkalispirochaeta","Salinispiraceae","Salinispira","SpirochaetaD","sg1","sf6",
                       "SpirochaetalesD","sdf1","SpirochaetalesE","SpirochaetaceaeB","Oceanispirochaeta","SpirochaetaE",
                       "SpirochaetaF","SpirochaetaG","sg2","Treponematales","Treponemataceae","Treponema",
                       "TreponemaB","TreponemaC","TreponemaD","TreponemaF","tg7","TreponemataceaeB",
                       "TreponemaE","TreponemaG","TreponemaH","tg5","tf3","so11"))

net <- graph_from_data_frame(d = links, vertices = id, directed = T)

lay = layout.reingold.tilford(net)

plot(net, vertex.shape = "none", 
     vertex.label.font = 3,
     vertex.label.cex = 0.3,
     edge.arrow.size = 0.3,
     rescale = F,
     ylim = c(0.7,3.4),xlim = c(-8,23.2), asp = 0,
     layout = lay)

If everything works, it should look similar to this smaller tree. this one does not include the nodes of the code above:

example

This is my result of the big tree so far. It contains the nodes in the code above: example2

The only solution I came up with so far is to somehow fit the plot into the plot panel and save it as a SVG file. Afterwards, I edited it using inkscape: result

Phil
  • 7,287
  • 3
  • 36
  • 66
Mittens
  • 13
  • 4
  • Does this answer your question? [Error in plot.new() : figure margins too large, Scatter plot](https://stackoverflow.com/questions/23050928/error-in-plot-new-figure-margins-too-large-scatter-plot) – dario Nov 11 '21 at 12:19
  • Displaying the plot in principle is working, but nothing is readable since the labels are overlapping or only a part is visible. I tried resizing the plot panel, but the plot is still too big. Now I'm trying around with the margins. So far, it's not working. – Mittens Nov 11 '21 at 12:52

1 Answers1

2

You can plot the tree graph as a dendrogram. Drawing it left to right allows to put all of the taxon labels in a readable column:

library(igraph)
net <- graph_from_data_frame(d = links, vertices = id, directed = F)
lay <- layout.reingold.tilford(net)

plot_dendrogram(cluster_fast_greedy(net))

enter image description here

There is also:

links %>%
  graph_from_data_frame() %>%
  as_adjacency_matrix() %>%
  dist() %>%
  hclust() %>%
  as.dendrogram() %>%
  plot()

enter image description here

danlooo
  • 10,067
  • 2
  • 8
  • 22
  • I tried this as well, but unfortunately it is not showing the right connections between the taxa. – Mittens Nov 11 '21 at 12:45
  • @Mittens Does my second code produce the right connections? – danlooo Nov 11 '21 at 12:57
  • Unfortunately not. I added an example of one of my smaller trees in my question. – Mittens Nov 11 '21 at 13:02
  • Your example data does not contain the node `Spirochaetales`. Only `SpirochaetalesA` or `SpirochaetalesC` – danlooo Nov 11 '21 at 13:05
  • Yes that's true. It is not included in the big tree. The example image was meant to show the principle structure of the tree. There have to be different levels – Mittens Nov 11 '21 at 13:13
  • I need to have the desired output of the example data, otherwise I can not check if my answer is correct. All of my answers show different levels.. – danlooo Nov 11 '21 at 13:16
  • I added my output tree to my question – Mittens Nov 11 '21 at 13:32