The fonts and size of a tree displayed by ggtree
1
2
Entering edit mode
6.6 years ago
sunyeping ▴ 110

I'm trying to use R package ggtree to visualize my polygenic tree. I use the following codes:

library(ggtree)
library(treeio)
tree_text<-read.table("tree.nwk")
tree <-read.tree(text=as.character(tree_text$V1))
ggtree(tree, layout="daylight") + geom_tiplab(aes(angle=angle))

Some tip labels of the tree are out of the plot region and invisible (Please refer to https://www.dropbox.com/s/87gyusx7ay1xxu8/tree.pdf?dl=0). I use the dev.size("in") command to get the size of the graphic window, and it returns: [1] 5.760417 5.750000. I wish the tree can be displayed in a A4 paper size region, so I try to make a larger graphic window by:

windows(height=8,width=8)
ggtree(tree, layout="daylight") + geom_tiplab(aes(angle=angle))

but it deoesn't work. The dev.size("in") command still returns: [1] 5.760417 5.750000, and some tip labels of the tree are still out of the plot region. If I use ggsave to save the image:

p<-ggtree(tree, layout="daylight") + geom_tiplab(aes(angle=angle))    
ggsave("tree.eps",p,height=8,width=8)

it gives an error:

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
      family 'sans' not included in postscript() device

If I save the image as pdf file:

postscript("tree.pdf",family="Times",fonts="sans")

the image can be saved, but some tip labels of the tree are still out of the plot region. I try to increase the height and width value in ggsave, but the size of tree increases with the plot canvas, so invisible tip labels are always invisible

Do you know how to correct the problem and display all the complete tip labels of the tree? I will be grateful to any help!

R • 14k views
ADD COMMENT
1
Entering edit mode

How about changing the height and width while saving in pdf format. something like this ...

ggsave(filename = "tree.pdf" , device = "pdf", width = 15, height = 30 , units = "in" , limitsize = FALSE)
ADD REPLY
0
Entering edit mode

Have you tried to just modify the label size with geom_tiplab?

ADD REPLY
1
Entering edit mode
3.7 years ago
ronoevan ▴ 20

For the size of the plot, you can adjust with either + xlim(NA, 24) and adjust the size as you prefer or + theme(plot.margin = unit(c(14,8,14,8), "mm")). For the font in tip labelling: you cant try + geom_tiplab(size =3) and adjust the size as you want.

tree2 <- tree2 + xlim(NA, 24)
#or

tree2 <- tree2 + theme(plot.margin = unit(c(14,8,14,8), "mm"))

#In this case 14mm, 8mm, 14mm and 8mm can be your starting settings. By either increasing or reducing individual settings of margins as you like till you get an optimised plot. 

tree2 <- tree2 + theme(plot.margin = unit(c(10,8,10,8), "mm"))

tree2 <- tree2 + theme(plot.margin = unit(c(20,10,20,10), "mm"))

tree2 <- tree2 +  geom_tiplab(size =3) #To adjust font size for tip labelling as you want.

Best,

Evans

ADD COMMENT

Login before adding your answer.

Traffic: 2023 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6