LSHTM_analysis/scripts/functions/lineage_dist.R

77 lines
3 KiB
R

###############################
# TASK: function to plot lineage
# dist plots with or without facet
# think about color palette
# for stability
##############################
#n_colours = length(unique(lin_dist_plot$duet_scaled))
#my_palette <- colorRampPalette(c(mcsm_red2, mcsm_red1, mcsm_mid, mcsm_blue1, mcsm_blue2))(n = n_colours+1)
lineage_distP <- function(plotdf
, x_axis = "duet_scaled"
, y_axis = "lineage_labels"
, x_lab = "DUET"
, all_lineages = F
, use_lineages = c("L1", "L2", "L3", "L4")
, with_facet = F
, facet_wrap_var = "" # FIXME: document what this is for
, fill_categ = "mutation_info_labels"
, fill_categ_cols = c("#E69F00", "#999999")
, label_categories = c("R", "S")
, my_ats = 15 # 15 axis text size
, my_als = 20 # 20 axis label size
, my_leg_ts = 16 #16
, my_leg_title = 16 #16
, my_strip_ts = 20 #20
, leg_pos = c(0.8, 0.9)
, leg_pos_wf = c("top", "left", "bottom", "right")
, leg_dir_wf = c("horizontal", "vertical")
, leg_label = "Mutation Group"
, alpha = 0.7)
{
if(!all_lineages){
plotdf = plotdf[plotdf[[y_axis]]%in%use_lineages,]
}
ggplot(plotdf, aes_string(x = x_axis
, y = y_axis))+
geom_density_ridges(aes_string(fill = fill_categ)
, scale = 3
, size = 0.3
, alpha = alpha) +
scale_x_continuous(expand = c(0.01, 0.01)) +
#coord_cartesian( xlim = c(-1, 1)) +
scale_fill_manual(values = fill_categ_cols
, labels = label_categories) +
theme(axis.text.x = element_text(size = my_ats
, angle = 90
, hjust = 1
, vjust = 0.4)
, axis.text.y = element_text(size = my_ats)
, axis.title.x = element_text(size = my_ats)
, axis.title.y = element_blank()
, strip.text = element_text(size = my_strip_ts)
, legend.text = element_text(size = my_leg_ts)
, legend.key.size = unit(my_leg_ts, 'pt')
, legend.title = element_text(size = my_leg_title)
, legend.position = c(0.8, 0.9)) +
labs(x = x_lab
, fill = leg_label) +
# FIXME: This didn't work BEFORE i fixed the ggplot() assignment thing!!!
if (with_facet){
# used reformulate or make as formula
#fwv = reformulate(facet_wrap_var)
fwv = as.formula(paste0("~", facet_wrap_var))
facet_wrap(fwv) +
theme(legend.position = leg_pos_wf
, legend.direction = leg_dir_wf)
}
}