R/annotation_bar_plot.R

Defines functions plot_selection_frequencies

Documented in plot_selection_frequencies

#' A bar plot generator function
#'
#' This function allows you to plot a bar plot of frequencies of a selected column of the list of annotation dataframes provided
#' @param samples a list of data frames of peaks annotations
#' @param selection the name of the column that you want to select to plot the frequencies of the values contained in that column
#' @param plot_dir the directory in which you want the plots, default = "plots"
#' @keywords frequencies
#' @import tidyverse ggsci
#' @export
#' @examples
#' plot_selection_frequencies(samples, column_name)
plot_selection_frequencies <- function(samples, selection, plot_dir = "plots") {
  counter <<- 0
  dir.create(file.path(plot_dir), showWarnings = FALSE)
  pl_selection <- lapply(samples, function(condition){
    counter <<- counter + 1
    condition <- condition %>%
      select(Tier, Role_in_Cancer, cpg_island, repetitive_sequence) %>%
      select(!!selection) %>%
      group_by(!!! rlang::syms(selection)) %>%
      summarise(count = n()) %>%
      mutate(prop = count / sum(count) * 100)
  #    mutate(feature = !!as.name(selection) %>% fct_infreq() %>% fct_rev()) %>%
  #    ggplot(aes(feature)) +
  #      geom_bar() +
  #      ggtitle(names(samples[count])) +
  #      xlab(selection)
    ggplot(data = condition) +
      geom_bar(aes(x = !!as.name(selection), y = prop), stat = "identity", colour = pal_npg()(nrow(condition)), fill = pal_npg("nrc", alpha = 0.8)(nrow(condition))) +
      ggtitle(names(samples)[counter]) +
      theme(axis.text = element_text( size = rel(0.4), angle = 25))
      #theme_bw()
  })
  pdf(file.path(plot_dir, paste("selection_frequencies_", selection, ".pdf", sep = "")))
  do.call(grid.arrange, c(pl_selection, nrow = round(length(samples)/2)))
  dev.off()
}
riccardo-trozzo/BlissR documentation built on Aug. 1, 2020, 12:23 a.m.