R/theme.R
9f34e866
 ##' tree theme
 ##'
3c7f66c8
 ##' 'theme_tree' defines a blank background to display tree
e785220c
 ##'
9f34e866
 ##' @title theme_tree
3c7f66c8
 ##' @param bgcolor set background color, defaults to "white"
9f34e866
 ##' @param ... additional parameter
827cd9ec
 ##' @importFrom ggplot2 theme_void
9f34e866
 ##' @importFrom ggplot2 theme
 ##' @importFrom ggplot2 element_blank
0ae9e98a
 ##' @importFrom ggplot2 xlab
 ##' @importFrom ggplot2 ylab
9f34e866
 ##' @export
 ##' @return updated ggplot object with new theme
d3057fa1
 ##' @author Guangchuang Yu
9f34e866
 ##' @examples
 ##' require(ape)
 ##' tr <- rtree(10)
 ##' ggtree(tr) + theme_tree()
827cd9ec
 theme_tree <- function(bgcolor="white", ...) {
 
fd3b93fe
     list(xlab(NULL),
          ylab(NULL),
          theme_tree2_internal() +
          theme(panel.background=element_rect(fill=bgcolor, colour=bgcolor),
                axis.line.x = element_blank(),
                axis.text.x = element_blank(),
                axis.ticks.x = element_blank(),
                ...)
          )
     
     ## theme_void() +
     ##     theme(panel.background=element_rect(fill=bgcolor, colour=bgcolor),
     ##           ...)
9f34e866
 }
 
d3057fa1
 ##' dendrogram theme
 ##'
 ##'
 ##' @title theme_dendrogram
 ##' @inheritParams theme_tree
3c7f66c8
 ##' @param fgcolor set color of axis
d3057fa1
 ##' @export
 ##' @importFrom ggplot2 element_text
 ##' @author Guangchuang Yu
827cd9ec
 theme_dendrogram <- function(bgcolor = "white", fgcolor = "black", ...) {
     theme_tree2(bgcolor = bgcolor,
                 axis.line.x = element_blank(),
                 axis.text.x = element_blank(),
                 axis.ticks.x = element_blank(),
                 axis.line.y = element_line(color=fgcolor),
                 axis.text.y = element_text(color=fgcolor),
                 axis.ticks.y = element_line(color=fgcolor),
                 ...)
d3057fa1
 }
 
 
9f34e866
 ##' tree2 theme
 ##'
3c7f66c8
 ##' 'theme_tree2' supports displaying phylogenetic distance by setting x-axis
e785220c
 ##'
9f34e866
 ##' @title theme_tree2
3c7f66c8
 ##' @param bgcolor set background color, defaults to "white"
 ##' @param fgcolor set foreground color, defaults to "black"
9f34e866
 ##' @param ... additional parameter
 ##' @importFrom ggplot2 theme_bw
 ##' @importFrom ggplot2 theme
 ##' @importFrom ggplot2 element_blank
 ##' @importFrom ggplot2 element_line
 ##' @importFrom ggplot2 element_rect
 ##' @export
 ##' @return updated ggplot object with new theme
d3057fa1
 ##' @author Guangchuang Yu
9f34e866
 ##' @examples
 ##' require(ape)
 ##' tr <- rtree(10)
 ##' ggtree(tr) + theme_tree2()
 theme_tree2 <- function(bgcolor="white", fgcolor="black", ...) {
fd3b93fe
     list(xlab(NULL),
          ylab(NULL),
          theme_tree2_internal(bgcolor, fgcolor, ...)
          )
0ae9e98a
 }
 
827cd9ec
 ##' @importFrom ggplot2 theme_bw
e785220c
 theme_tree2_internal <- function(bgcolor="white", fgcolor="black",
ecf50e8f
                                  legend.position="right",
e785220c
                                  panel.grid.minor=element_blank(),
                                  panel.grid.major=element_blank(),
                                  panel.border=element_blank(),
                                  axis.line.y=element_blank(),
                                  axis.ticks.y=element_blank(),
                                  axis.text.y=element_blank(),...) {
9416b852
     ## need to set axis.line otherwise the setting cannot be inherited.
2b9d5e9d
     ## https://github.com/GuangchuangYu/ggtree/issues/218
827cd9ec
 
     theme_bw() +
a92aaed1
         theme(legend.position=legend.position,
               panel.grid.minor=panel.grid.minor,
               panel.grid.major=panel.grid.major,
               panel.background=element_rect(fill=bgcolor, colour=bgcolor),
               panel.border=panel.border,
2b9d5e9d
               axis.line=element_line(color=fgcolor),
               ##axis.line.x=element_line(color=fgcolor),
a92aaed1
               axis.line.y=axis.line.y,
               axis.ticks.y=axis.ticks.y,
               axis.text.y=axis.text.y,
               ...)
9f34e866
 }
 
245479db
 
 ##' inset theme
 ##'
 ##' theme for inset function
 ##' @title theme_inset
3c7f66c8
 ##' @param legend.position set the position of legend
245479db
 ##' @param ... additional parameter
 ##' @return ggplot object
 ##' @export
 ##' @author Guangchuang Yu
dbf4890a
 theme_inset <- function(legend.position =  "none", ...) {
245479db
     list(xlab(NULL),
          ylab(NULL),
5ebd35a4
          theme_tree(legend.position = legend.position, plot.margin = unit(c(0, 0, 0, 0), "lines"), ...),
a695ddf7
          ggfun::theme_transparent()
245479db
          )
 }