LSHTM_analysis/scripts/functions/lineage_dist.R

75 lines
2.7 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 = ""
, fill_categ = "mutation_info_labels"
, fill_categ_cols = c("#E69F00", "#999999")
, my_ats = 15 # axis text size
, my_als = 20 # axis label size
, my_leg_ts = 16
, my_leg_title = 16
, my_strip_ts = 20
, leg_pos = c(0.8, 0.9)
, leg_pos_wf = c("top", "left", "bottom", "right")
, leg_dir_wf = c("horizontal", "vertical")
, leg_label = "")
{
if(!all_lineages){
plotdf = plotdf[plotdf[[y_axis]]%in%use_lineages,]
}
LinDistP = ggplot(plotdf, aes_string(x = x_axis
, y = y_axis))+
geom_density_ridges(aes_string(fill = fill_categ)
, scale = 3
, size = 0.3
, alpha = 0.8) +
scale_x_continuous(expand = c(0.01, 0.01)) +
#coord_cartesian( xlim = c(-1, 1)) +
scale_fill_manual(values = fill_categ_cols) +
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.title = element_text(size = my_leg_title)
, legend.position = c(0.8, 0.9)) +
labs(x = x_lab
, fill = leg_label)
if (with_facet){
# used reformulate or make as formula
#fwv = reformulate(facet_wrap_var)
fwv = as.formula(paste0("~", facet_wrap_var))
LinDistP = LinDistP +
facet_wrap(fwv) +
theme(legend.position = leg_pos_wf
, legend.direction = leg_dir_wf)
}
return(LinDistP)
}