YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/contribution-tree-data/
838 stars 173 forks source link

chunky resolution of circular tree segments #614

Open ulo opened 4 months ago

ulo commented 4 months ago

Hi! I love to use the circular ggtree layouts, but always found the low line segment resolution - especially close to the root - somewhat unattractive:

image

Apparently that is a gneral issue with ggplot polar plots: https://stackoverflow.com/questions/9483033/increase-polygonal-resolution-of-ggplot-polar-plots

Following the stackoverflow post from above, overwriting the ggplot2:::coord_munch function solves the issue:

# Save the original version of coord_munch
coord_munch_old <- ggplot2:::coord_munch

# Make a wrapper function that has a different default for segment_length
coord_munch_new <- function(coord, data, range, segment_length = 1/500, is_closed = FALSE) {
  coord_munch_old(coord, data, range, segment_length, is_closed)
}
# Make the new function run in the same environment
environment(coord_munch_new) <- environment(ggplot2:::coord_munch)

# Replace ggplot2:::coord_munch with coord_munch_new
assignInNamespace("coord_munch", coord_munch_new, ns="ggplot2")

This results in much nicer line segments:

image

Maybe you could implement this somehow in your great library?

Also, if I understood the stackoverflow discussion correctly, then the low resolution would be less of an issue if the numerical range of x-values would be higher (which is only between 0 and 0.5 in my circular trees). Maybe increasing this scale is an easier solution compared to overwriting some ggplot2 function?

Cheers, Ulrich