R/heorTheme.R

Defines functions heorTheme

Documented in heorTheme

#' Add heor theme to ggplot, David's version, TBC
#'
#' This function adds updated heor styling done by David, not complete yet.
#' TBD: make sure font dependencies are working correctly.
#' At the moment, it requires to import a bunch of windows fonts through extrafonts library (will it come as a part of installation?),
#' import takes a few minutes.
#' @param data.name The dataset used in the plot
#' @param font The name of the font you want to be used, e.g. font="Tahoma"
#' @param fontscale The size of the font to be used in the plot
#' @param colour.name The name of the variable that is going to be stratified by colours
#' @examples
#' data(line_plot)
#' line_plot$treatment <- as.character(line_plot$treatment)
#' line_plot$treatment <- factor(line_plot$treatment,levels=unique(line_plot$treatment))
#' ggplot() +
#' geom_line(data=line_plot,mapping=aes(x=time,y=hba1c,colour=treatment),linetype="solid",alpha=1.0,size=0.7) +
#' geom_hline(yintercept=c(8.0,9.0),colour="black",linetype="dashed",size=0.4) +
#' scale_x_continuous("Time (Years)",breaks=seq(0,40,5),limits=c(0,40),expand=c(0,0)) +
#' scale_y_continuous("HbA1c (%)",breaks=seq(6.5,10.0,0.5),limits=c(6.5,10.0),expand=c(0,0)) +
#' labs(title="Plot Title",
#'      subtitle="Plot Subtitle",
#'      caption="Example caption text",
#'      tag="A)") +
#'      heorTheme(line_plot,"Tahoma",1,"treatment")
#' @family themes
#' @export

heorTheme <- function(data.name,font,fontscale,colour.name) {

  # load in font
  windowsFonts(manual=windowsFont(font))

  # determine number of colour levels
  n.colour <- length(levels(data.name$colour.name))

  # determine colour set
  if (n.colour <= 4) {
    colour.val <- c("#0B1A3B","#AA75EB","#2E5BFF","#A6A6A6")
  } else {
    colour.val <- brewer.pal(n.colour,"Paired")
  }

  # determine legend name
  legend.colour.name <- paste(toupper(substring(colour.name,1,1)),
                              substring(colour.name,2,nchar(colour.name)),
                              " :",
                              sep="")

  # colour set and theme - need ggplot v3.2.1
  list(scale_colour_manual(legend.colour.name,values=colour.val),
       theme(axis.line.x=element_line(colour="black",linetype="solid",lineend="square",size=0.2),
             axis.line.y=element_line(colour="black",linetype="solid",lineend="square",size=0.2),
             axis.ticks.x=element_line(colour="black",linetype="solid",lineend="square",size=0.2),
             axis.ticks.y=element_line(colour="black",linetype="solid",lineend="square",size=0.2),
             #axis.ticks.length.x=unit(0.1,"cm"),
             #axis.ticks.length.y=unit(0.1,"cm"),
             axis.text.x.bottom=element_text(colour="black",family="manual",size=14*fontscale,face="plain",
                                             hjust=0.5,vjust=1.0,margin=margin(3,0,0,0),angle=0),
             axis.text.x.top=element_text(colour="black",family="manual",size=14*fontscale,face="plain",
                                          hjust=0.5,vjust=0.0,margin=margin(0,0,3,0),angle=0),
             axis.text.y.left=element_text(colour="black",family="manual",size=14*fontscale,face="plain",
                                           hjust=1.0,vjust=0.5,margin=margin(0,3,0,0),angle=0),
             axis.text.y.right=element_text(colour="black",family="manual",size=14*fontscale,face="plain",
                                            hjust=0.0,vjust=0.5,margin=margin(0,0,0,3),angle=0),
             axis.title.x.bottom=element_text(colour="black",family="manual",size=14*fontscale,face="bold",
                                              hjust=0.5,vjust=1.0,margin=margin(10,0,0,0),angle=0),
             axis.title.x.top=element_text(colour="black",family="manual",size=14*fontscale,face="bold",
                                           hjust=0.5,vjust=0.0,margin=margin(0,0,10,0),angle=0),
             axis.title.y.left=element_text(colour="black",family="manual",size=14*fontscale,face="bold",
                                            hjust=0.5,vjust=1.0,margin=margin(0,10,0,0),angle=90),
             axis.title.y.right=element_text(colour="black",family="manual",size=14*fontscale,face="bold",
                                             hjust=0.5,vjust=0.0,margin=margin(0,0,0,10),angle=270),
             legend.position="bottom",
             legend.box="vertical",
             legend.direction="horizontal",
             legend.justification="centre",
             legend.background=element_blank(),
             legend.margin=margin(5,5,5,5),
             legend.key=element_blank(),
             legend.key.height=unit(1,"cm"),
             legend.key.width=unit(1,"cm"),
             legend.spacing.x=unit(0.1,"cm"),
             legend.spacing.y=unit(0.1,"cm"),
             legend.text=element_text(colour="black",family="manual",size=12*fontscale,face="plain",
                                      vjust=0.5,margin=margin(0,10,0,0),angle=0),
             legend.text.align=0.0,
             legend.title=element_text(colour="black",family="manual",size=12*fontscale,face="bold",
                                       vjust=0.5,margin=margin(0,10,0,0),angle=0),
             legend.title.align=0.0,

             panel.background=element_blank(),
             panel.border=element_rect(colour="black",linetype="solid",fill=NA,size=0.2),
             panel.spacing.x=unit(1.0,"cm"),
             panel.spacing.y=unit(1.0,"cm"),
             panel.grid.major.x=element_line(colour="grey95",linetype="solid",lineend="square",size=0.2),
             panel.grid.major.y=element_line(colour="grey95",linetype="solid",lineend="square",size=0.2),
             panel.grid.minor.x=element_blank(),
             panel.grid.minor.y=element_blank(),
             plot.background=element_blank(),
             plot.margin=unit(c(0.2,0.4,0.2,0.2),"cm"),
             plot.title=element_text(colour="black",family="manual",size=12*fontscale,face="bold",
                                     hjust=0.5,vjust=0.5,margin=margin(0,0,5,0),angle=0),
             plot.subtitle=element_text(colour="black",family="manual",size=12*fontscale,face="plain",
                                        hjust=0.5,vjust=0.5,margin=margin(0,0,5,0),angle=0),
             plot.caption=element_text(colour="black",family="manual",size=10*fontscale,face="plain",
                                       hjust=0.0,vjust=0.5,margin=margin(0,0,0,0),angle=0),
             plot.tag=element_text(colour="black",family="manual",size=12*fontscale,face="bold",
                                   hjust=0.0,vjust=0.5,margin=margin(0,0,0,0),angle=0),
             plot.tag.position="topleft",
             strip.placement="inside",
             strip.background.x=element_rect(colour="black",linetype="solid",fill="grey90",size=0.2),
             strip.background.y=element_rect(colour="black",linetype="solid",fill="grey90",size=0.2),
             strip.text.x=element_text(colour="black",family="manual",size=12*fontscale,face="italic",
                                       hjust=0.5,vjust=0.5,margin=margin(5,5,5,5),angle=0),
             strip.text.y=element_text(colour="black",family="manual",size=12*fontscale,face="italic",
                                       hjust=0.5,vjust=0.5,margin=margin(5,5,5,5),angle=270))
  )

}
Alik-V/heor documentation built on April 4, 2020, 9:38 p.m.