knitr::opts_chunk$set( dev = 'svg', collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4 )
ml-viewR is a visualization package for machine learning analyses. The functions in mlview
are named according to the package which they are to be used with, whereas generic functions start with "mlview.".
This package contains functions used for visualization of ropls
objects.
mlview
currently supports all models created by ropls
, including PCA, PLS, OPLS, PLS-DA and OPLS-DA models.
The ropls.plot
function can be used to create score (default) and loading plots. This function allows colour by group and the addition of group ellipses. Hotelling's T2 ellipse can be toggled on or off. The ggplot output from ropls.plot
can be further modified if required.
The ropls.enrich
function performs fgsea
enrichment on a matrix of variables, variable information and a single numeric comlumn. Furthermore, the column(s) used in the enrichment analysis can be specified. This function returns the fgsea
output, including enriched groups, enrichment scores and p-values.
The functions in mlviewer
starting with "ropls" (e.g. ropls.plot, ropls.enrich) can be used to visualize the results from models created using the package ropls
.
To install mlviewer from GitHub run the following code:
library(devtools) install_github("JeffreyMolendijk/ml-viewR")
To get started we need to load some packages.
library(mlviewer) library(ropls) library(ggplot2) library(plotly) library(dplyr) library(fgsea) library(rlang) library(shiny)
The mtcars dataset is used for this example. We will create a PCA, OPLS and OPLS-DA model using ropls
.
To create the PCA model we supply the data matrix, but no y-variable. For the OPLS model we supply a continuous variable ("mpg"), and set the number of predictive and orthogonal components to 1. For the OPLS-DA model we supply a categorical variable with two groups ("am"). Regular PLS and PLS-DA models can be made by setting the number of orthogonal components to 0.
#Load mtcars dataset data.x = mtcars #Make models ropls.pca = opls(x = mtcars, predI = 2, info.txtC = NULL, fig.pdfC = FALSE) ropls.opls = opls(x = mtcars %>% select(-mpg), y = mtcars$mpg, predI = 1, orthoI = 1, info.txtC = NULL, fig.pdfC = FALSE) ropls.oplsda = ropls1 = opls(x = mtcars %>% select(-am), y = as.factor(mtcars$am), predI = 1, orthoI = 1, info.txtC = NULL, fig.pdfC = FALSE)
Produce ggplot images of ropls
models using ropls.plot.
ropls.plot(ropls.pca, xvar = "p1", yvar = "p2", hotelling = TRUE, ellipse = FALSE) ropls.plot(ropls.pca, xvar = "p1", yvar = "p2", hotelling = TRUE, ellipse = FALSE, col.pca = data.x$mpg) ropls.plot(ropls.pca, xvar = "p1", yvar = "p2", hotelling = TRUE, ellipse = FALSE, col.pca = as.factor(data.x$am)) ropls.plot(ropls.pca, plottype = "loading", xvar = "p1", yvar = "p2", hotelling = TRUE, ellipse = FALSE) ropls.plot(ropls.opls, xvar = "p1", yvar = "o1", hotelling = TRUE) ropls.plot(ropls.opls, xvar = "p1", yvar = "o1", hotelling = TRUE, plottype = "loading") ropls.plot(ropls.oplsda, xvar = "p1", yvar = "o1", hotelling = TRUE) ropls.plot(ropls.oplsda, xvar = "p1", yvar = "o1", hotelling = TRUE, ellipse = TRUE) ropls.plot(ropls.oplsda, xvar = "p1", yvar = "o1", hotelling = TRUE, ellipse = TRUE) %>% ggplotly()
Combining mlviewer with plotly allows the user to study the individual scores and loadings more easily.
ropls.plot(ropls.oplsda, xvar = "p1", yvar = "o1", hotelling = TRUE, ellipse = TRUE) %>% ggplotly()
# basic example shinyApp( ui = fluidPage( selectInput("plot", "Plot:", c("PCA, scoreplot, no colour" = "p1", "PCA, scoreplot, continuous colour" = "p2", "PCA, scoreplot, categorical colour" = "p3")), plotOutput('plot') ), server = function(input, output) { output$plot <- renderPlot({ if (input$plot == "p1") { ropls.plot(ropls.pca, xvar = "p1", yvar = "p2", hotelling = TRUE, ellipse = FALSE) } else if (input$plot == "p2") { ropls.plot(ropls.pca, xvar = "p1", yvar = "p2", hotelling = TRUE, ellipse = FALSE, col.pca = data.x$mpg) } else if (input$plot == "p3") { ropls.plot(ropls.pca, xvar = "p1", yvar = "p2", hotelling = TRUE, ellipse = FALSE, col.pca = as.factor(data.x$am)) } }) } )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.