Mittens
Mittens

Reputation: 13

R: Plot too large to fit into window

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

Upvotes: 1

Views: 1333

Answers (1)

danlooo
danlooo

Reputation: 10627

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

Upvotes: 2

Related Questions