#' Visualize the results as a stacked bar chart with tidyverse/ggplot2.
#'
#' @param input input result of `CIBERSORT` or `quantiseq`
#' @param id patient identifier of input
#' @param title plot title
#' @param legend.position legend position
#' @param coord_filp logical variables, `coord_filp` of ggplot2
#' @param palette default is palette3
#' @param show_col default is FALSE
#' @param cols default is NULL, user can define colors manually
#' @param features default is NULL, user can define which column to draw
#' @param pattern patterns of features
#'
#' @return
#' @export
#'
#' @examples
#' # Loading TCGA-STAD microenvironment data
#' data("sig_stad", package = "IOBR")
#' # showing 20 tumor microenvironment cell proportion deconvoluted by CIBERSORT algorithm
#' cell_bar_plot(input = sig_stad[1:20, ], id = "ID", features = colnames(sig_stad)[25:46])
cell_bar_plot<- function(input, id = "ID", title = "Cell Fraction", features = NULL, pattern = NULL, legend.position = "bottom",
coord_filp = TRUE, palette = 3, show_col = F, cols = NULL){
input<-as.data.frame(input)
colnames(input)[which(colnames(input)==id)]<-"ID"
if(is.null(features)){
if(is.null(pattern)) stop(">>>=== The 'pattern' parameter must be defined...")
feas <- colnames(input)[str_detect(colnames(input), pattern = pattern)]
}else{
feas <- features
}
input <- input[, c("ID", feas)]
input<-remove_names(input_df = input, variable = "colnames", patterns_to_na = patterns_to_na, patterns_space = "_")
##################
if(legend.position == "top"|legend.position=="bottom") {
legend.direction<-"horizontal"
}else{
legend.direction<-"vertical"
}
if(!is.null(cols)){
cols<-cols
}else{
if(is.null(palette)){
cols<-IOBR::palettes(category = "random", palette = 4, show_col = show_col, show_message = T)
}else{
cols<-IOBR::palettes(category = "random", palette = palette, show_col = show_col, show_message = T)
}
}
if(coord_filp){
pp<-input %>%
tidyr::gather(cell_type,fraction, -ID) %>%
# plot as stacked bar chart
ggplot(aes(x=ID, y=fraction, fill=cell_type)) +
geom_bar(stat='identity') +
coord_flip() +
theme_light()+
scale_fill_manual(values = cols) +
scale_x_discrete(limits = rev(levels(input)))+
ggtitle(paste0(title))+
theme(plot.title=element_text(size=rel(2),hjust=0.5),
axis.text.x= element_text(face="plain",angle=0,hjust = 1,color="black"),
axis.text.y= element_text(face="plain",angle= 30,hjust = 1,color="black"))+
theme(legend.title = element_blank(),
legend.position= legend.position,
legend.direction= legend.direction,
legend.justification=c(.5,.5),
legend.box="horizontal",
legend.box.just="top")
}else{
pp<- input %>%
tidyr::gather(cell_type,fraction, -ID) %>%
# plot as stacked bar chart
ggplot(aes(x=ID, y=fraction, fill=cell_type)) +
geom_bar(stat='identity') +
# coord_flip() +
theme_light()+
scale_fill_manual(values = cols) +
scale_x_discrete(limits = rev(levels(input)))+
ggtitle(paste0(title))+
theme(plot.title=element_text(size=rel(2),hjust=0.5),
axis.text.x= element_text(face="plain",angle=0,hjust = 1,color="black"),
axis.text.y= element_text(face="plain",angle=30,hjust = 1,color="black"))+
theme(legend.title = element_blank(),
legend.position= legend.position,
legend.direction= legend.direction,
legend.justification=c(.5,.5),
legend.box="horizontal",
legend.box.just="top")
}
print(pp)
return(pp)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.