############################# # Barplots: ggplot # stats +/- # violin +/- # barplot +/ # beeswarm ############################# lf_bp <- function(lf_df = lf_duet , p_title = "DUET-DDG" , colour_categ = "outcome" , x_grp = "mutation_info_labels" , y_var = "param_value" , facet_var = "param_type" , n_facet_row = 1 , y_scales = "free_y" , colour_bp_strip = "khaki2" , dot_size = 3 , dot_transparency = 0.3 , violin_quantiles = c(0.25, 0.5, 0.75) # can be NULL , my_ats = 11 # axis text size , my_als = 10 # axis label size , my_fls = 10 # facet label size , my_pts = 11 # plot title size) , make_boxplot = FALSE , bp_width = c("auto", 0.5) , add_stats = TRUE , stat_grp_comp = c("R", "S") , stat_method = "wilcox.test" , my_paired = FALSE , stat_label = c("p.format", "p.signif") ) { fwv = as.formula(paste0("~", facet_var)) #fwv = reformulate(facet_var) p1 <- ggplot(lf_df, aes_string(x = x_grp, y = y_var)) + facet_wrap( fwv , nrow = n_facet_row , scales = y_scales) + geom_violin(trim = T , scale = "width" #, position = position_dodge(width = 0.9) , draw_quantiles = violin_quantiles) if (make_boxplot){ if (bp_width == "auto"){ bp_width = 0.5/length(unique(lf_df[[x_grp]])) cat("\nAutomatically calculated boxplot width, using bp_width:\n", bp_width, "\n") }else{ cat("\nBoxplot width value provided, using:", bp_width, "\n") bp_width = bp_width} p2 = p1 + geom_boxplot(fill = "white" , outlier.colour = NA #, position = position_dodge(width = 0.9) , width = bp_width) + geom_quasirandom(#priority = "density" #, shape = 21 size = dot_size , alpha = dot_transparency , show.legend = FALSE , cex = 0.8 , aes( colour = factor( eval( parse( text = colour_categ ) ) ) ) ) + ggplot2::scale_color_manual(values = consurf_colours) } else { #Legend=factor(eval(parse(text = colour_categ))) # ggbeeswarm (better than geom_point) p2 = p1 + #theme(legend.title=element_text('XXX')) + # Legend doesn't need a title) geom_quasirandom(#priority = "density" #, shape = 21 size = dot_size , alpha = dot_transparency , show.legend = FALSE # , fast = FALSE , cex = 0.8 , aes( colour = factor( eval( parse( text = colour_categ ) ) ) ) #, aes(colour = Legend) ) + ggplot2::scale_color_manual(values = consurf_colours) } # Add foramtting to graph OutPlot = p2 + theme(axis.text.x = element_text(size = my_ats) , axis.text.y = element_text(size = my_ats , angle = 0 , hjust = 1 , vjust = 0) , axis.title.x = element_text(size = my_ats) , axis.title.y = element_text(size = my_ats) , plot.title = element_text(size = my_pts , hjust = 0.5 , colour = "black" , face = "bold") , strip.background = element_rect(fill = colour_bp_strip) , strip.text.x = element_text(size = my_fls , colour = "black") , legend.title = element_text(color = "black" , size = my_als) , legend.text = element_text(size = my_ats) , legend.direction = "vertical") + labs(title = p_title , x = "" , y = "") if (add_stats){ my_comparisonsL <- list( stat_grp_comp ) OutPlot = OutPlot + stat_compare_means(comparisons = my_comparisonsL , method = stat_method , paired = my_paired , label = stat_label[2]) return(OutPlot) } return(OutPlot) }