Density plots display the distribution of meta-data values corresponding to peaks observed in one or more samples, a group, or pair of groups. The densityPlot function can display histograms or smoothed density, and it can display both together if plotting a single distribution (i.e. one sample or one aggregated group). The distribution of any numeric column of the e_meta element may be plotted.

To construct a density plot for a single sample, first construct a subset containing only the sample of interest. By default, densityPlot plots the distribution (without a histogram) of the chosen variable.

library(ftmsRanalysis)
data("exampleProcessedPeakData")

one_sample <- subset(exampleProcessedPeakData, samples="EM0011_sample")
densityPlot(one_sample, variable="NOSC")

Setting the plot_hist parameter to TRUE will display both the curve and histogram.

densityPlot(one_sample, variable="NOSC", plot_hist=TRUE)

Alternatively, using plot_curve=FALSE gives just a histogram. The yaxis parameter controls the scale of the y-axis: it can be either “density” (default) or “count”

densityPlot(one_sample, variable="NOSC", plot_hist=TRUE, plot_curve=FALSE, yaxis="count")

The densityPlot function can also choose a single sample from a larger dataset, using the samples parameter:

densityPlot(exampleProcessedPeakData, variable="NOSC", samples="EM0069_sample")

The samples parameter may also be used to specify more than one sample, although only curves may be plotted, not histograms.

densityPlot(exampleProcessedPeakData, variable="NOSC", samples=c("EM0011_sample", "EM0013_sample", "EM0015_sample"),
            curve_colors=c("blue", "red", "green"))

Since the exampleProcessedPeakData object has a group definition (see group_designation for how to construct this), the groups parameter can be used to select multiple groups to plot (these curves are the distribution combining all peaks observed by samples in the group).

getGroupDF(exampleProcessedPeakData)
#>         SampleID Group Location Crop.Flora
#> 1  EM0011_sample   M_S        M          S
#> 2  EM0013_sample   M_S        M          S
#> 3  EM0015_sample   M_S        M          S
#> 4  EM0017_sample   M_S        M          S
#> 5  EM0019_sample   M_S        M          S
#> 6  EM0061_sample   M_C        M          C
#> 7  EM0063_sample   M_C        M          C
#> 8  EM0065_sample   M_C        M          C
#> 9  EM0067_sample   M_C        M          C
#> 10 EM0069_sample   M_C        M          C
#> 11 EW0111_sample   W_C        W          C
#> 12 EW0113_sample   W_C        W          C
#> 13 EW0115_sample   W_C        W          C
#> 14 EW0117_sample   W_C        W          C
#> 15 EW0119_sample   W_C        W          C
#> 16 EW0161_sample   W_S        W          S
#> 17 EW0163_sample   W_S        W          S
#> 18 EW0165_sample   W_S        W          S
#> 19 EW0167_sample   W_S        W          S
#> 20 EW0169_sample   W_S        W          S
densityPlot(exampleProcessedPeakData, variable="NOSC", samples=FALSE, groups=c("M_C", "M_S"))

The samples and groups parameters work in similar ways to select multiple distributions to plot. An NA value indicates that all samples or groups in the data object should be plotted, a FALSE value suppresses curves, and a character vector may be used to select specific samples and/or groups. The samples parameter is NA by default (so it will plot all samples found) and groups is FALSE by default (do not plot groups).

The easiest way to plot all samples in a group, and the group aggregate is to subset down by group, then call densityPlot. (Recall that all samples are plotted by default so only the groups parameter must be specified.)

MSgroup <- subset(exampleProcessedPeakData, groups="M_S")
densityPlot(MSgroup, variable="NOSC", groups=NA)