Module 7 Chart objects

  • Define chart objects
  • Learn about methods specific to chart objects
  • plot a chart for example DatasetExperiment and model objects

7.1 The chart template

The chart template provides a mechanism to generate standardised graphics for input objects. Most frequently charts are designed for DatasetExperiment objects and specific model objects, like PCA.

7.1.1 chart methods

The chart template defines a key method for all charts: chart_plot.

This method defines the chart to be plotted. Like for model objects, this method is provided by extending the chart template. For example, the pca_scores_plot chart object provides a chart_plot method that takes a PCA object as input and generates a PCA scores plot from it.

# prepare chart object
C = pca_scores_plot(factor_name = 'Species')

# plot chart C for model M
chart_plot(C,M)

The factor_name input specifies a column of the sample_meta data from the iris dataset to use as a basis for colouring the groups in the plot.

The scores being plotted are formatted as a DatasetExperiment object, and the meta data has been inherited from the input data (iris data in this case).

You could use the scatter_chart object with M$scores as the second input for chart_plot to obtain a similar plot.

7.2 Modifying charts

Charts can be defined for any input object, not just models. For example, the DatasetExperiment_factor_boxplot chart generates a boxplot for a named column of a DatasetExperiment object. Here, we use it to generate a boxplot of the Petal.Width column and separate/colour the boxes according to the Species factor.

# prepare chart object
C = DatasetExperiment_factor_boxplot(
        feature_to_plot = 'Petal.Width',
        factor_names = 'Species')

# plot C for iris data
chart_plot(C,iris_DatasetExperiment())

Sometimes, you will want to make changes to the plot, such as adding titles, axis labels, legend position, etc. The output for all chart objects is a ggplot object, so you can add to it after the chart_plot call. For example, here we add the missing y-axis label.

# plot C for iris data
chart_plot(C,iris_DatasetExperiment()) + ylab('Petal.Width')

If you want to make more complex changes, or generate your own plots, then you will need to e.g. use ggplot and extract data from the objects yourself. If you use the chart a lot, consider wrapping it into a new chart object; refer to the struct package vignettes here if you are interested in how to do this.

7.3 Exercise

PCA scores plot for MTBLS79 data

In this exercise you will use chart objects to explore the effects of processing on the MTBLS79 dataset. Use the default inuts for each object unless pecified.