LSHTM_analysis/scripts/functions/stability_count_bp.R

80 lines
3.1 KiB
R

#!/usr/bin/env Rscript
#########################################################
# TASK: function for basic barplot returning stability counts
#########################################################
# load libraries and functions
library(ggplot2)
theme_set(theme_grey())
#==========================================================
# stability_count_bp(): basic barplots for stability counts
# input args
## df containing data to plot
## df column name containing stability outcome
## legend title
## ...opt args
#==========================================================
stability_count_bp <- function(plotdf
, df_colname = ""
, leg_title = ""
, ats = 25 # axis text size
, als = 22 # axis label size
, lts = 20 # legend text size
, ltis = 22 # label title size
, geom_ls = 10 # geom_label size
, yaxis_title = "Number of nsSNPs"
, bp_plot_title = ""
, label_categories #= c("LEVEL1", "LEVEL2")
, title_colour = "chocolate4"
, subtitle_text = NULL
, sts = 20
, subtitle_colour = "#350E20FF" #brown
#, leg_position = c(0.73,0.8) # within plot area
, leg_position = "top"
, bar_fill_values = c("#F8766D", "#00BFC4")){
# convert to factor and get labels
plotdf[[df_colname]] = as.factor(plotdf[[df_colname]])
label_categories = levels(plotdf[[df_colname]])
#OutPlot_count = ggplot(plotdf, aes(x = eval(parse(text = df_colname)))) +
OutPlot_count = ggplot(plotdf, aes_string(x = df_colname)) +
geom_bar(aes(fill = eval(parse(text = df_colname)))
, show.legend = TRUE) +
geom_label(stat = "count"
, aes(label = ..count..)
, color = "black"
, show.legend = FALSE
, size = geom_ls) +
theme(axis.text.x = element_blank()
, axis.title.x = element_blank()
, axis.title.y = element_text(size = als)
, axis.text.y = element_text(size = ats)
, legend.position = leg_position
, legend.text = element_text(size = lts)
, legend.title = element_text(size = ltis)
, plot.title = element_text(size = als
, colour = title_colour
, hjust = 0.5)
, plot.subtitle = element_text(size = sts
, hjust = 0.5
, colour = subtitle_colour)) +
labs(title = bp_plot_title
, subtitle = subtitle_text
, y = yaxis_title) +
# scale_fill_discrete(name = leg_title
# , labels = label_categories) +
scale_fill_manual(name = ""
# name = leg_title
, values = bar_fill_values
, labels = label_categories # problem with consurf decreasing level
)
return(OutPlot_count)
}
#############################################################
# end of function
#############################################################