From 5cbaef3d3664fd3c06431bc487d312576ce586d2 Mon Sep 17 00:00:00 2001 From: Tanushree Tunstall Date: Tue, 9 Aug 2022 18:15:54 +0100 Subject: [PATCH] various heat-bar/position tile faff --- scripts/functions/bp_subcolours.R | 12 ++-- scripts/functions/logoP_or.R | 90 ++++++++++++++------------ scripts/minimal-heat-line-example.R | 27 ++++++++ scripts/plotting/position_annotation.R | 59 +++++++++++++++++ 4 files changed, 140 insertions(+), 48 deletions(-) create mode 100644 scripts/minimal-heat-line-example.R create mode 100644 scripts/plotting/position_annotation.R diff --git a/scripts/functions/bp_subcolours.R b/scripts/functions/bp_subcolours.R index 9dc7607..462a2c8 100755 --- a/scripts/functions/bp_subcolours.R +++ b/scripts/functions/bp_subcolours.R @@ -2,7 +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") +#source("~/git/LSHTM_analysis/scripts/functions/generate_distance_colour_map.R") ColourPalleteMulti = function(df, group, subgroup){ @@ -121,16 +121,16 @@ bp_stability_hmap <- function(plotdf = merged_df3 , 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), 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), + , linetype = "blank"), #end of distance-heat-bar generate_distance_legend(plotdf), ncol = 2, #align = "hv", diff --git a/scripts/functions/logoP_or.R b/scripts/functions/logoP_or.R index 278b48b..b47c375 100644 --- a/scripts/functions/logoP_or.R +++ b/scripts/functions/logoP_or.R @@ -69,17 +69,17 @@ LogoPlotCustomH <- function(plot_df # Data processing for logo plot ################################# plot_df = generate_distance_colour_map(plot_df, debug=TRUE) - + unique_colour_map = unique(plot_df[,c("position","ligD_colours")]) unique_colour_map = unique_colour_map[order(unique_colour_map$position), ] rownames(unique_colour_map) = unique_colour_map$position - + unique_colour_map2 = unique_colour_map unique_colour_map2$position=as.factor(unique_colour_map2$position) unique_colour_map2$ligD_colours = as.factor(unique_colour_map2$ligD_colours) - + if (rm_empty_y){ plot_df = plot_df[!is.na(plot_df[y_axis_colname]),] cat("\nRemoving empty positions...\n") @@ -171,20 +171,18 @@ LogoPlotCustomH <- function(plot_df , seq_type = "aa") + #ylab("my custom height") + theme( axis.ticks.x = element_blank() + , axis.ticks.length = unit(0, "pt") , axis.title.x = element_blank() - , axis.text.x = element_blank() # turn this off and the below on if you want to visually + # , axis.text.x = element_blank() # turn this off and the below on if you want to visually # verify positions. - # , axis.text.x = element_text(size = x_ats - # , angle = x_tangle - # , hjust = 1 - # , vjust = 0.4 - # , colour = xfont_bgc - # , margin = margin(b=0) - # ) + , axis.text.x = element_text(size = x_ats + , angle = x_tangle + , colour = xfont_bgc + , vjust = 0.4 + , margin = margin(t=-8,r=0,b=0,l=0, unit="mm") + ) , axis.text.y = element_text(size = y_ats , angle = y_tangle - , hjust = 1 - , vjust = 0 , colour = yfont_bgc) , axis.title.y = element_text(size = y_tts , colour = ytt_col) @@ -195,10 +193,11 @@ LogoPlotCustomH <- function(plot_df , legend.position = leg_pos , legend.direction = leg_dir - , plot.background = element_rect(fill = theme_bgc) + #, plot.background = element_blank() , plot.margin = margin(b=0) , panel.grid=element_blank() - , panel.background = element_rect(fill = theme_bgc) + , plot.background = element_rect(fill = theme_bgc, colour=NA) + , panel.background = element_rect(fill = "transparent", colour=NA) )+ @@ -217,36 +216,43 @@ LogoPlotCustomH <- function(plot_df , fill = position , colour = position , linetype = "blank" - ) + ) ) + geom_tile() + - theme(axis.text.x = element_text(size = x_ats - , angle = x_tangle - , hjust = 1 - , vjust = 0.4 - , colour = xfont_bgc) - , axis.text.y = element_blank() - , axis.ticks.y = element_blank() - , axis.title.x = element_text(size = x_tts - , colour = xtt_col) - , axis.title.y = element_text(size = y_tts - , colour = ytt_col) - , legend.title = element_text(size = leg_tts - , colour = ytt_col) - , legend.text = element_text(size = leg_ts) - - , legend.position = leg_pos - , legend.direction = leg_dir - , plot.background = element_rect(fill = theme_bgc) - , plot.margin = margin(t=0) - , panel.grid=element_blank() - , panel.background = element_rect(fill = theme_bgc) + theme( + axis.text.x = element_blank() + # axis.text.x = element_text(size = x_ats + # , angle = x_tangle + # , hjust = 1 + # , vjust = 0.4 + # , colour = xfont_bgc) + , axis.text.y = element_blank() + , axis.ticks.y = element_blank() + , axis.ticks.x = element_blank() + , axis.title.x = element_blank() + , axis.title.y = element_text(size = y_tts + , colour = ytt_col) + , legend.title = element_text(size = leg_tts + , colour = ytt_col) + , legend.text = element_text(size = leg_ts) + + , legend.position = leg_pos + , legend.direction = leg_dir + #, plot.background = element_blank() + , plot.margin = margin(t=0,b=0) + , panel.grid=element_blank() + , plot.background = element_rect(fill = theme_bgc, colour=NA) + , panel.background = element_rect(fill = "transparent", colour=NA) + #, panel.background = element_rect(fill = theme_bgc) + #, panel.background = element_blank() ) + - scale_x_discrete(x_lab, labels=factor(unique_colour_map$position)) + + #scale_x_discrete(x_lab, labels=factor(unique_colour_map$position)) + scale_color_manual(values=unique_colour_map$ligD_colours) + scale_fill_manual(values=unique_colour_map$ligD_colours) + - labs(y = NULL), - ncol=1, align='v', rel_heights = c(8/10,1/10) + labs(y = NULL), NULL, + position_annotation(plot_df, bg=theme_bgc), + + ncol=1, align='v', rel_heights = c(16,1,-0.1, 1) ) - } -#LogoPlotCustomH(merged_df3) \ No newline at end of file +} +#LogoPlotCustomH(small_df3) diff --git a/scripts/minimal-heat-line-example.R b/scripts/minimal-heat-line-example.R new file mode 100644 index 0000000..b75a671 --- /dev/null +++ b/scripts/minimal-heat-line-example.R @@ -0,0 +1,27 @@ +#minimal example + +, tpos0 = 0 # 0 is a magic number that does my sensible default +, tW0 = 1 +, tH0 = 0.3 + + +plot_df = generate_distance_colour_map(plot_df, yvar_colname = stability_colname, debug=TRUE) + + +cowplot::plot_grid( + your_working_ggpot_statement + 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") + + , + generate_distance_legend(plot_df, yvar_colname = stability_colname) + ncol = 2, + #align = "hv", + rel_widths = c(9/10, 0.4/10) +) + + + + diff --git a/scripts/plotting/position_annotation.R b/scripts/plotting/position_annotation.R new file mode 100644 index 0000000..cd39cf1 --- /dev/null +++ b/scripts/plotting/position_annotation.R @@ -0,0 +1,59 @@ +# position_annotation takes a Data Frame (df) and returns a ggplot object. +# +# This plots position tiles for the (up to) three ligands as well as drug +position_annotation=function(plot_df, bg="transparent"){ + x_ats = 12 + x_tangle = 90 + x_tts = 20 + y_tts = 23 + xtt_col = "black" + ytt_col = "black" + leg_dir = "horizontal" + leg_ts = 15 + leg_tts = 16 + leg_pos = "none" + + #plot_df = generate_distance_colour_map(plot_df, debug=TRUE) + + ggplot(plot_df, + aes(x=factor(position), # THIS STUPID FUCKING FACTOR THING + ) + ) + + geom_tile(aes(y=0, fill= bg_all, colour = bg_all) + ) + + geom_tile(aes(y=1, fill= col_bg1, colour = col_bg1) + ) + + geom_tile(aes(y=2, fill= col_bg2, colour = col_bg2) + ) + + geom_tile(aes(y=3, fill= col_bg3, colour = col_bg3) + ) + + + scale_x_discrete("Position", labels=factor(plot_df$position)) + + scale_color_manual(values = c( + "brown"="brown", + "purple"="purple", + "transparent"="transparent", + "blue"="blue", "cyan"="cyan", + "cornflowerblue"="cornflowerblue" + ), + expand=c(0,0) + ) + + scale_fill_manual(values = c( + "brown"="brown", + "purple"="purple", + "transparent"="transparent", + "blue"="blue", + "cyan"="cyan", + "cornflowerblue"="cornflowerblue" + ), + expand=c(0,0) + ) + + #scale_x_continuous(expand=c(0,0)) + + #scale_y_continuous(expand=c(0,0)) + + theme_nothing() + + + theme(plot.background = element_rect(fill = bg, colour=NA), + plot.margin = margin(t=0,b=0)) + + labs(x = NULL, y = NULL) +} +