Dimension Reduction - Plot - Component Plot Extension

From Q
Jump to navigation Jump to search
This page is currently under construction, or it refers to features which are under development and not yet available for use.
This page is under construction. Its contents are only visible to developers!


Chart the first two components or factors from a principal components analysis or factor analysis (created, for example, with Insert > More > Dimension Reduction > Principal Components Analysis).

Example

Example output:


Example input:


Options

Principal components analysis An R Output containing a factor analysis or principal components analysis. This does not need to be created with Insert > More > Dimension Reduction > Principal Components Analysis (you can make your own factor analysis or PCA), but the item does need to have a property called loadings.

Show variable labels on plot Whether or not the labels found in the loadings will be displayed in the chart.

Code

includeWeb("QScript R Output Functions");
var is_displayr = (!!Q.isOnTheWeb && Q.isOnTheWeb());
// Separate selected item from the group/page
var selected_items = project.report.selectedRaw();
var selected_item = selected_items[0];
var selected_group = selected_item.type == "ReportGroup" ? selected_item : selected_item.group;
// Add custom R code
var output_name = generateUniqueRObjectName("component.plot");
var r_expr = "library(flipDimensionReduction)\n" + 
             output_name + " <- ComponentPlot(inputItem, show.labels = formShowLabels)";
if (is_displayr) {
        try {
            var new_output = selected_group.group.appendPage("Title and Content");
        } catch(e) {
            var new_output = selected_group.group.appendPage("TitleOnly");
        }
        new_output.subItems[0].text = "Component Plot";
        new_output.name = "Component Plot";
        var component_plot = new_output.appendR(r_expr);
        selected_group.group.moveAfter(new_output, selected_group);
        project.report.setSelectedRaw([new_output]);
    } else {
        var component_plot = selected_group.appendR(r_expr);
        selected_group.moveAfter(component_plot, selected_item);
        project.report.setSelectedRaw([component_plot]);
    }
// Check if TextPCA output is used to update the variable label control as required
var not_text_pca = selected_item.outputClasses.indexOf("TextPCA") < 0 ? 'true' : 'false'
// Create GUI controls
var javascript_inputs_expr = 'form.setHeading("Component Plot");\n' +
                             'form.dropBox({name: "inputItem", label: "Principal components analysis", types: ["R"],\n' +
                             '              prompt: "Output of PCA"});\n' +
                             'form.checkBox({name: "formShowLabels", label: "Show variable labels on plot", default_value: ' + not_text_pca + '});';
component_plot.setCodeForGuiControls(javascript_inputs_expr);
// Use the selected flipFactorAnalysis output as the input to the plot.
component_plot.setGuiControlInputRaw("inputItem", selected_item.guid);