ahmohamed / lipidr

Data Mining and Analysis of Lipidomics datasets in R
https://www.lipidr.org/
Other
27 stars 13 forks source link

plot_molecules in vignette: colors for molecule and class do not match #27

Closed nilshoffmann closed 2 years ago

nilshoffmann commented 2 years ago

Hi, in the targeted workflow vignette (https://www.lipidr.org/articles/workflow.html) the calls to plot_molecules produce a mismatch between the Molecule name (e.g. on species level) and the Class that is used for the fill color: plot_molecules(d_qc, "sd", measure = "Retention Time", log = FALSE)

Example1

I traced this to an issue with the order of scale_x_discrete(labels=as.character(dlong$Molecule)): https://github.com/ahmohamed/lipidr/blob/master/R/plot.R#L293 This also appears in the other related plotting functions for cv and boxplot within plot_molecules.

The following (simplified) code seems to produce the expected result for the boxplot example:

ddf <- lipidr:::to_long_format(d)
ggplot(data=ddf, mapping=aes(x=Molecule, y=log2(Area), fill=Class)) + geom_boxplot() + facet_wrap(~filename, scales = "free_y") + coord_flip()

grafik

ahmohamed commented 2 years ago

Hi @nilshoffmann,

Very sorry for the delay. I'm embarrassed I've completely missed this issue. Thanks for spotting this. Looking at git blame, I can see scale_x_discrete(labels=as.character(dlong$Molecule)) was added to address issues where the dataset contains duplicate molecules. This happens in targeted MRMs when transitions are analysis individually, or more commonly in untargeted experiments, where ambiguous molecules are identifies multiple times (at different RTs). the simplified solution (which is similar to the old code as well) combines these duplicate molecules on the plot, although they are treated separately in lipidr (for example in de_anlaysis). Unfortunately specifying labels as character vector doesn't play well with faceting as you already noticed.

I've pushed a different solution which generates labels using a lambda function. Quickly checking, I can see the issue is resolved. Please let me know if you see otherwise.

Thanks again Nils. Cheers, Ahmed.