# Dimension Reduction - Principal Components Analysis Biplot

Principal Components Analysis Biplots (PCA Biplot) is a principal components analysis of the table itself, where each column is a variable. It produces a scatterplot showing how the rows (columns) differ in terms of the column (rows) attributes. For more information on how to interpret the chart see Principal Components Analysis Biplot

## How to Create

1. In Displayr: Insert > More > Dimension Reduction > Principal Components Analysis Biplot
2. In Q: Create > Dimension Reduction > Principal Components Analysis Biplot
2. Under Inputs > Input table select a table or click Paste or type table to enter in table manually

## Example

Example output Scatterplot:

Example output Moonplot:
For more information on how to interpret a moonplot see this blog post.

Example input:

## Options

Input table The name of a table containing data to be analyzed. The table should only contain a single statistic (e.g., Total %).

Paste or type table Opens up a blank spreadsheet into which tabular data can be manually entered or pasted. This is an alternative to Input table.

Normalization The method used to normalize the coordinates of the chart (this changes the chart, but not the outputs of analysis). The default method is Principal which charts the principal coordinates (i.e., the standard coordinates multiplied by the singular values). Row principal and Column principal chart the standard coordinates of the columns (rows) against the principal coordinates.

Output:

Scatterplot
Moonplot
Text

Rows to ignore, Columns to ignore The names of any rows or columns to be removed from the table prior to analysis.

Row title, Column title Title of the row/column attributes.

Row attribute color, Column attribute color Color of the points shown in the labelled scatterplot.

When using this feature you can obtain additional information that is stored by the R code which produces the output.

1. To do so, select Create > R Output.
2. In the R CODE, paste: item = YourReferenceName
3. Replace YourReferenceName with the reference name of your item. Find this in the Report tree or by selecting the item and then going to Properties > General > Name from the object inspector on the right.
4. Below the first line of code, you can paste in snippets from below or type in str(item) to see a list of available information.

For a more in depth discussion on extracting information from objects in R, checkout our blog post here.

## Code

var allow_control_groups = Q.fileFormatVersion() > 10.9; // Group controls for Displayr and later versions of Q
var heading_text = 'PCA Biplot of a Table';
if (!!form.setObjectInspectorTitle)
else
function isEmpty(x) { return (x == undefined || x.getValue() == null && (x.getValues() == null || x.getValues().length == 0)) }
function isBlankSheet(x) { return (x.getValue() == null || x.getValue().length == 0) }

var controls = [];

if (!form.dataEntry)
{
var tableInput = form.dropBox({label: "Input table", types:["table", "RItem"], name: "formTableToAnalyse",  multi : false});
controls.push(tableInput);
}
else
{
var tableInput = form.dropBox({label: "Input table", types:["table", "RItem"], name: "formTableToAnalyse",  multi : false, required: false,
prompt: "Table of data to analyze"});
var pasteInput = form.dataEntry({label: "Paste or type table", name: "formEnteredData", prompt: "Opens a spreadsheet into which you can paste data.", required: true, large_data_error: "The data entered is too large. The best alternative is to add your data as a Data Set, use Table > Raw Data > Variable(s), and connect that table to this analysis."})

if (!allow_control_groups || !isEmpty(tableInput) || isBlankSheet(pasteInput))
controls.push(tableInput);
if (!allow_control_groups || !isBlankSheet(pasteInput) || isEmpty(tableInput))
controls.push(pasteInput);
}
var norm = form.comboBox({label: "Normalization", alternatives:["Principal", "Row principal", "Column principal", "Symmetrical", "None"], name: "formNormalization",
multi : false, default_value: "Principal", prompt: "Scaling of coordinated by singular values"});
controls.push(norm);
var outOpt = form.comboBox({label: "Output", alternatives:["Scatterplot", "Moonplot", "Text"], name: "formOutput",  multi : false, default_value: "Scatterplot"});
controls.push(outOpt);
var rowIgnore = form.textBox({label: "Rows to ignore", type: "text", default_value: "NET, Total, SUM", name: "formIgnoreRows", required: false,
prompt: "Rows of the table that are excluded from the analysis"});
controls.push(rowIgnore);
var colIgnore = form.textBox({label: "Columns to ignore", type: "text", default_value: "NET, Total, SUM", name: "formIgnoreColumns", required: false,
prompt: "Columns of the table that are excluded from the analysis"});
controls.push(colIgnore);
var rowTitle = form.textBox({label: "Row title", type: "text", default_value: "Rows", name: "formRowTitle", required: false});
controls.push(rowTitle);
var colTitle = form.textBox({label: "Column title", type: "text", default_value: "Columns", name: "formColumnTitle", required: false});
controls.push(colTitle);
if (outOpt.getValue() == "Scatterplot")
{
var rowColor = form.colorPicker({label: "Row attributes color", name: "rowColor", default_value:"#5B9BD5"});
controls.push(rowColor);
var colColor = form.colorPicker({label: "Column attributes color", name: "colColor", default_value:"#ED7D31"});
controls.push(colColor);
}
form.setInputControls(controls);

library(flipTransformations)
library(flipDimensionReduction)

x <- get0("formTableToAnalyse")
if (is.null(x))
x <- flipTransformations::ParseEnteredData(formEnteredData, want.data.frame = TRUE, want.col.names = TRUE, want.row.names = TRUE)

attr(x, "row.column.names") <- c(formRowTitle, formColumnTitle)
row.color <- ifelse(formOutput=="Scatterplot", rowColor, "")
col.color <- ifelse(formOutput=="Scatterplot", colColor, "")

biplot <- PrincipalComponentsBiplot(x,
normalization = formNormalization,
output = formOutput,
row.names.to.remove = formIgnoreRows,
column.names.to.remove = formIgnoreColumns,
row.color = row.color,
col.color = col.color)