R/formatting.R

Defines functions format_chr_for_plot format_ggaes

Documented in format_chr_for_plot format_ggaes

#' Format Chr for Plot
#'
#' @param chrom.lengths
#'
#' @return
#' @export
#'
#' @examples
format_chr_for_plot <- function(chrom.lengths){
chrom.lengths <-
  c(
    249250621,
    243199373,
    198022430,
    191154276,
    180915260,
    171115067,
    159138663,
    146364022,
    141213431,
    135534747,
    135006516,
    133851895,
    115169878,
    107349540,
    102531392,
    90354753,
    81195210,
    78077248,
    59128983,
    63025520,
    48129895,
    51304566,
    155270560,
    59373566
  )

names(chrom.lengths) <- c(paste("chr", 1:22, sep = ""),
                          "chrX",
                          "chrY")

chrom.names <- names(chrom.lengths)

chrom.abspos.end <- cumsum(chrom.lengths)
names(chrom.abspos.end) <- chrom.names
chrom.abspos.start <-
  c(0, chrom.abspos.end[1:(length(chrom.abspos.end) - 1)])
names(chrom.abspos.start) <- names(chrom.abspos.end)

# Creating data frame object for chromosome rectangles shadows
chrom.rects <- data.frame(xstart = chrom.abspos.start,
                          xend = chrom.abspos.end)
xbreaks <- rowMeans(chrom.rects)
chrom.rects$colors <-rep(c("white", "gray"), 12)

#chromosomes squares background
ggchr.back <-
  list(geom_rect(
    data = chrom.rects,
    aes(
      xmin = xstart,
      xmax = xend,
      ymin = -Inf,
      ymax = Inf,
      fill = colors
    ),
    alpha = .2
  ), scale_fill_identity())

}

#' Format ggaes
#'
#' @param ggchr.back
#'
#' @return
#' @export
#'
#' @examples
format_ggaes <- function(ggchr.back){
  sec_breaks <- c(0, 0.5e9, 1e9, 1.5e9, 2e9, 2.5e9, 3e9)
  sec_labels <- c(0, 0.5, 1, 1.5, 2, 2.5, 3)

  ggaes <-
    list(
    scale_x_continuous(breaks = rowMeans(ggchr.back[[1]]$data[c("xstart", "xend")]),
                       labels = gsub("chr", "", rownames(ggchr.back[[1]]$data)),
                       position = "top",
                       expand = c(0,0),
                       sec.axis = sec_axis(~., breaks = sec_breaks, labels = sec_labels, name = "Genome Position (Gb)")),
    theme_classic(),
    theme(
      axis.text.x = element_text(angle = 0,
                                 vjust = .5,
                                 size = 8),
      axis.text.y = element_text(size = 8),
      axis.title.y.right = element_text(margin = margin(l = 10)),
      legend.position = "none",
      axis.ticks.x = element_blank(),
      axis.title = element_text(size = 8),
      plot.title = element_text(size = 8),
      axis.text = element_text(size = 8)
    )
  )
}
whtns/varbintools documentation built on Dec. 1, 2019, 5:15 a.m.