fix many plot functions to stop them using the "g=ggplot()" pattern,

which annoyingly throws away lots of useful data that RShiny needs for
clickable plots. Also split the "flame bar" for ligand distance out into
separate functions in generate_distance_colour_map.R. This can now be
easily incorporated into any "wide" graph showing all positions.
This commit is contained in:
Tanushree Tunstall 2022-08-03 18:56:14 +01:00
parent e498d46f8b
commit bdbc97c40a
8 changed files with 1323 additions and 725 deletions

View file

@ -2,6 +2,7 @@
# 1b: Define function: coloured barplot by subgroup
# LINK: https://stackoverflow.com/questions/49818271/stacked-barplot-with-colour-gradients-for-each-bar
#########################################################
source("~/git/LSHTM_analysis/scripts/functions/generate_distance_colour_map.R")
ColourPalleteMulti = function(df, group, subgroup){
@ -35,6 +36,7 @@ ColourPalleteMulti = function(df, group, subgroup){
bp_stability_hmap <- function(plotdf = merged_df3
, xvar_colname = "position"
, yvar_colname = 'duet_scaled' #FIXME: temp, remove
#, bar_col_colname = "group"
, stability_colname = "duet_scaled"
, stability_outcome_colname = "duet_outcome"
@ -46,8 +48,27 @@ bp_stability_hmap <- function(plotdf = merged_df3
, my_pts = 20 # plot-title size
, my_xlab = "Position"
, my_ylab = "No. of nsSNPs"
# Custom 2: x-axis: geom tiles ~ lig distance
#, A_xvar_lig = T
, lig_dist_colname = LigDist_colname # from globals
, tpos0 = 0 # 0 is a magic number that does my sensible default
, tW0 = 1
, tH0 = 0.2
)
{
################################################
# Custom 2: x-axis geom tiles ~ lig distance
################################################
#=========================
# Build data with colours
# ~ ligand distance
#=========================
plotdf = generate_distance_colour_map(plotdf, yvar_colname = stability_colname, debug=TRUE)
# order the df by position and ensure it is a factor
plotdf = plotdf[order(plotdf[[xvar_colname]]), ]
@ -74,38 +95,45 @@ bp_stability_hmap <- function(plotdf = merged_df3
cat("\nNo. of sub colours generated:", length(subcols_bp_hmap))
#-------------------------------
# Generate the subcols barplot
#-------------------------------
#g = ggplot(plotdf, aes(x = factor(position, ordered = T)))
g = ggplot(plotdf, aes_string(x = xvar_colname
# , ordered = T)
))
OutWidePlot = g + geom_bar(aes(fill = group)
, colour = "grey") +
scale_fill_manual( values = subcols_bp_hmap
, guide = "none") +
theme( axis.text.x = element_text(size = my_xaxls
, angle = 90
, hjust = 1
, vjust = 0.4)
, axis.text.y = element_text(size = my_yaxls
, angle = 0
cowplot::plot_grid(
ggplot(plotdf, aes_string(x = xvar_colname
# , ordered = T)
)) +
geom_bar(aes(fill = group)
, colour = "grey") +
scale_fill_manual( values = subcols_bp_hmap
, guide = "none") +
theme( axis.text.x = element_text(size = my_xaxls
, angle = 90
, hjust = 1
, vjust = 0)
, axis.title.x = element_text(size = my_xaxts)
, axis.title.y = element_text(size = my_yaxts )
, plot.title = element_text(size = my_pts
, hjust = 0.5)) +
labs(title = p_title
, x = my_xlab
, y = my_ylab)
return(OutWidePlot)
, vjust = 0.4)
, axis.text.y = element_text(size = my_yaxls
, angle = 0
, hjust = 1
, vjust = 0)
, axis.title.x = element_text(size = my_xaxts)
, axis.title.y = element_text(size = my_yaxts )
, plot.title = element_text(size = my_pts
, hjust = 0.5)) +
geom_tile(aes(, tpos0 # heat-mapped distance tiles along the bot
, width = tW0
, height = tH0)
, fill = plotdf$ligD_colours
, colour = plotdf$ligD_colours
, linetype = "blank") +
labs(title = p_title
, x = my_xlab
, y = my_ylab),
generate_distance_legend(plotdf, yvar_colname = stability_colname),
ncol = 2,
#align = "hv",
rel_widths = c(9/10, 0.4/10)
)
}