R/theme_set.r

Defines functions add_axes_umap theme_umap_ge theme_classic_ge theme_bw_ge getTheme geUseTheme

Documented in add_axes_umap geUseTheme theme_bw_ge theme_classic_ge theme_umap_ge

#' @title geUseTheme
#'
#' @import ggplot2
#' @param theme ggplot themes (either "bw" or "classic")
#' @description Set the theme in the current session
#' @export
geUseTheme <- function(theme='bw'){
    figtheme <- switch(theme, 'bw'=theme_bw_ge(), 'classic'=theme_classic_ge())
    if(is.null(figtheme)){
        stop("Currently I only support bw or classic.")
    }
    theme_set(figtheme)
}

## Auxilary function to set the theme
getTheme <- function() theme(text = element_text(size=23,face='bold'),
          axis.line = element_line(size = 1, linetype = "solid"),
          axis.title.y=element_text(margin=margin(0,15,0,0)),axis.title.x=element_text(margin=margin(15,0,0,0)),
          plot.margin = unit(c(1,1,1,1), "cm"),
          plot.title = element_text(margin=margin(0,0,15,0), hjust=0.5))


#' @title theme_bw_ge
#'
#' @import ggplot2
#' @export
theme_bw_ge <- function(){
    figtheme <- theme_bw() +getTheme()
    figtheme
}

#' @title theme_classic_ge
#'
#' @import ggplot2
#' @export
theme_classic_ge <- function(){
    figtheme <- theme_classic() +getTheme()
    figtheme
}


#' @title theme_umap_ge
#'
#' @param aes_x for patchwork object generated by DimPlot, specify the reduction axis, e.g. aes_x="UMAP_1"
#' @param aes_y for patchwork object generated by DimPlot, specify the reduction axis, e.g. aes_y="UMAP_2"
#' @param vjust.x vertical adjustment for label on x axis
#' @param vjust.y vertical adjustment for label on y axis
#' @import ggplot2
#' @export
theme_umap_ge <- function(aes_x=NULL, aes_y=NULL, vjust.x=2, vjust.y=-1){
    basic <- list(
        geom_reduction_axes(color="black",
                            arrow=arrow(length = unit(0.3,"cm"), type = "closed")),
        geom_reduction_axes_label(label="UMAP 1", vjust=vjust.x),
        geom_reduction_axes_label(label="UMAP 2", axis=2, angle=90, vjust=vjust.y),
        theme_void()
    )
    if(is.null(aes_x) && is.null(aes_y)){
        basic
    }else if(!is.null(aes_x) && !is.null(aes_y)){
        list(aes_string(x=aes_x, y=aes_y),
                basic)
    }else(
        stop("aes_x and aes_y must both be provided")
    )
}


#' @title add_axes_umap
#'
#' @import ggplot2
#' @export
add_axes_umap <- function(g, vjust.x=1.2, vjust.y=1.2){
    gobj <- ggplot_build(g)

    o.x <- gobj$layout$panel_scales_x[[1]]$range$range[1]
    o.y <- gobj$layout$panel_scales_y[[1]]$range$range[1]

    offset <- diff(gobj$layout$panel_scales_x[[1]]$range$range)/10

    g +
        theme_void() +
        annotate(geom = "segment", x=o.x-offset, xend = o.x+offset,
                 y = o.y-offset, yend = o.y-offset,
                 arrow=arrow(length = unit(0.3,"cm"), type = "closed")) +
        annotate(geom = "segment", x=o.x-offset, xend = o.x-offset,
                 y = o.y-offset, yend = o.y+offset,
                 arrow=arrow(length = unit(0.3,"cm"), type = "closed")) +
        annotate(geom = "text", label="UMAP 1", x=o.x,
                 y = o.y-offset,
                 vjust=vjust.x) +
        annotate(geom = "text", label="UMAP 2", x=o.x-offset, angle=90,
                 y = o.y,
                 vjust=vjust.y)

}
lch14forever/ggessentials documentation built on Sept. 1, 2021, 7:23 a.m.