rggplot2ggtree

ggplot2 cannot find function "ggtitle"


I'm using ggtree, which uses ggplot2, to plot a phylogeny.

I'm using R version 4.3.1, with ggplot version 3.4.2, and ggtree 3.8.2.

the script (executed from a temp file) that gives the issue is very simple:

library(ggtree)
tree <- read.tree('fa/clustal.DEG20010516.input.newick')
svg('fa/phylo.svg')
ggtree(tree) + theme_tree2() + geom_tiplab(align=TRUE, linesize=0.5) +xlim(0,0.6) + ggtitle('DEG')
dev.off()

but this returns an error:

! The tree contained negative edge length. If you want to ignore the edge, you
can set `options(ignore.negative.edge=TRUE)`, then re-run ggtree.
Error in ggtitle("DEG") : could not find function "ggtitle"

I'm getting instructions from https://4va.github.io/biodatasci/r-ggtree.html

how can I set the title when the standard way doesn't work?


Solution

  • As @Seth and @Jon Spring said in the comments, you can't use the ggtitle() function unless you load the ggplot2 package. I can reproduce the problem using a newick alignment file from a previous project:

    library(ggtree)
    
    tree <- read.tree('~/Desktop/gisaid_mafft_tree.txt')
    svg('phylo.svg')
    ggtree(tree) + theme_tree2() + geom_tiplab(align=TRUE, linesize=0.5) +xlim(0,0.6) + ggtitle('DEG')
    #> Error in ggtitle("DEG"): could not find function "ggtitle"
    dev.off()
    #> quartz_off_screen 
    #>                 2
    

    Loading the ggplot2 library first solves the problem:

    library(ggplot2)
    library(ggtree)
    
    tree <- read.tree('~/Desktop/gisaid_mafft_tree.txt')
    svg('phylo.svg')
    ggtree(tree) + theme_tree2() + geom_tiplab(align=TRUE, linesize=0.5) +xlim(0,0.6) + ggtitle('DEG')
    dev.off()
    #> quartz_off_screen 
    #>                 2
    

    Created on 2023-08-16 with reprex v2.0.2