getwd() setwd("~/git/LSHTM_analysis/mcsm_analysis/pyrazinamide/scripts/plotting") getwd() ######################################################################## # Installing and loading required packages and functions # ######################################################################## source("../Header_TT.R") ######################################################################## # Read file: call script for combining df for PS # ######################################################################## source("../combining_two_df.R") #---------------------- PAY ATTENTION # the above changes the working dir #[1] "git/LSHTM_analysis/mcsm_analysis/pyrazinamide/scripts" #---------------------- PAY ATTENTION #========================== # This will return: # df with NA: # merged_df2 # merged_df3 # df without NA: # merged_df2_comp # merged_df3_comp #========================== ########################### # Data for DUET plots # you need merged_df3 # or # merged_df3_comp # since these have unique SNPs # I prefer to use the merged_df3 # because using the _comp dataset means # we lose some muts and at this level, we should use # as much info as available ########################### # uncomment as necessary #<<<<<<<<<<<<<<<<<<<<<<<<< # REASSIGNMENT my_df = merged_df3 #my_df = merged_df3_comp #<<<<<<<<<<<<<<<<<<<<<<<<< # delete variables not required rm(merged_df2, merged_df2_comp, merged_df3, merged_df3_comp) # quick checks colnames(my_df) str(my_df) # Ensure correct data type in columns to plot: need to be factor # sanity check is.factor(my_df$DUET_outcome) my_df$DUET_outcome = as.factor(my_df$DUET_outcome) is.factor(my_df$DUET_outcome) #[1] TRUE ######################################################################## # end of data extraction and cleaning for plots # ######################################################################## #========================== # Plot 2: Barplot with scores (unordered) # corresponds to DUET_outcome # Stacked Barplot with colours: DUET_outcome @ position coloured by # DUET outcome. This is a barplot where each bar corresponds # to a SNP and is coloured by its corresponding DUET_outcome #============================ #=================== # Data for plots #=================== #<<<<<<<<<<<<<<<<<<<<<<<< # REASSIGNMENT df = my_df #<<<<<<<<<<<<<<<<<<<<<<<<< rm(my_df) # sanity checks upos = unique(df$Position) # should be a factor is.factor(my_df$DUET_outcome) #[1] TRUE table(my_df$DUET_outcome) # should be -1 and 1 min(df$ratioDUET) max(df$ratioDUET) tapply(df$ratioDUET, df$DUET_outcome, min) tapply(df$ratioDUET, df$DUET_outcome, max) #****************** # generate plot #****************** # set output dir for plots getwd() setwd("~/git/Data/pyrazinamide/output/plots") getwd() my_title = "Protein stability (DUET)" # axis label size my_xaxls = 13 my_yaxls = 15 # axes text size my_xaxts = 15 my_yaxts = 15 # no ordering of x-axis g = ggplot(df, aes(factor(Position, ordered = T))) g + geom_bar(aes(fill = DUET_outcome), colour = "grey") + 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 , hjust = 1 , vjust = 0) , axis.title.x = element_text(size = my_xaxts) , axis.title.y = element_text(size = my_yaxts ) ) + labs(title = my_title , x = "Position" , y = "Frequency") # for sanity and good practice rm(df) #======================= end of plot # axis colours labels # https://stackoverflow.com/questions/38862303/customize-ggplot2-axis-labels-with-different-colors # https://stackoverflow.com/questions/56543485/plot-coloured-boxes-around-axis-label