biplot_2d: 2D biplot

Description Usage Arguments Details Examples

View source: R/biplot2Dim.R

Description

Generates a 2D biplot representing the default points/scores and loadings of an ordination object, such as a PCA produced by princomp.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
biplot_2d(ordination_object, ordination_method = "PCA",
  biplot_type = "default", rows_over_columns = 0.5, groups = NULL,
  vips = NULL, detach_arrows = TRUE, show_grid = TRUE, show_axes = TRUE,
  show_group_legend = FALSE, show_vip_legend = TRUE, show_arrows = TRUE,
  show_fitAnalysis = TRUE, main_lwd = 2, grid_cex = 1, grid_font = 3,
  grid_adj = 0.5, invert_coordinates = c(FALSE, FALSE), xlim = NULL,
  ylim = NULL, x_title = "", y_title = "", x_title_cex = 1,
  x_title_font = 2, y_title_cex = 1, y_title_font = 2, subtitle = NULL,
  subtitle_position = "bottomleft", subtitle_cex = 1.2,
  point_type = "point", point_pch = 1, point_size = 1,
  point_label = NULL, point_label_cex = 1, point_label_font = 3,
  point_label_adj = c(0.5, 0.5), point_label_adj_override = NULL,
  arrow_color = "darkorange", arrow_mim_dist = 0, arrow_length = 0.2,
  arrow_cex = 0.1, arrow_lwd = 2, arrow_label_cex = 1,
  arrow_label_color = "black", arrow_label_font = 1,
  arrow_label_adj = c(0.5, 0.5), arrow_label_adj_override = NULL,
  group_color = "black", group_star_cex = 1, group_ellipse_cex = 1,
  group_ellipse_axes = FALSE, group_label_cex = 1,
  group_legend_title = "groups", group_legend_title_pos = c(0.5, 0.85),
  group_legend_title_cex = 1, group_legend_title_font = 3,
  group_legend_title_adj = 0.5, group_legend_box_color = "white",
  group_legend_key_pch = 15, group_legend_key_cex = 1,
  group_legend_key_lwd = 1, group_legend_key_margin = 0.15,
  group_legend_text_margin = 0.25, group_legend_text_color = "black",
  group_legend_text_cex = 1, group_legend_text_font = 1,
  group_legend_text_adj = 0, vip_color = "black", vip_pch = c(0, 1, 5, 2,
  6, 4, 3), vip_cex = c(5, 5, 5, 5, 5, 3, 3), vip_lwd = 5, vip_font = 1,
  vip_adj = c(0.5, 0.5), vip_legend_title = "VIPs",
  vip_legend_title_pos = c(0.5, 0.85), vip_legend_title_cex = 1,
  vip_legend_title_font = 3, vip_legend_title_adj = 0.5,
  vip_legend_box_color = "white", vip_legend_key_cex = 0.8,
  vip_legend_key_margin = 0.15, vip_legend_text_cex = 1,
  vip_legend_text_font = 1, vip_legend_text_adj = 0,
  vip_legend_text_margin = 0.25, fitAnalysis_lwd = 3,
  fitAnalysis_screePlot_color = c("grey", "white"),
  fitAnalysis_stress_cex = 1, fitAnalysis_stress_lab_cex = 1,
  fitAnalysis_stress_axis_cex = 1, fitAnalysis_stress_p_color = "darkgrey",
  fitAnalysis_stress_l_color = "black", test_text = NULL,
  test_spacing_paragraph = 0.8, test_spacing_line = 0.8, test_cex = 1,
  test_font = 1, test_adj = 0.5, fit_into_main = FALSE, main_fig = c(0,
  1, 0, 1), group_legend_fig = c(0.8, 0.99, 0.6, 0.9),
  vip_legend_fig = c(0.78, 0.99, 0.3, 0.55), arrow_fig = c(0.69, 0.99, 0.01,
  0.31), fitAnalysis_fig = c(0.02, 0.35, 0.06, 0.25), test_fig = c(0, 0.3,
  0.8, 0.99), x_title_fig = c(0.25, 1, 0.85, 1), y_title_fig = c(0.91, 1, 0,
  1), output_type = c("preview", "png"), open_new_device = TRUE,
  leave_device_open = FALSE, directory = "", file_name = "2D Biplot",
  width = 400, height = 400, family = "sans")

Arguments

ordination_object

A R object containing a direct and named reference to default ordination outputs (i.e. ordination_object$scores or ordination_object$points, ordination_object$loadings and ordination_object$sdev) available for at least 2 dimensions. Alternatively, a data frame or matrix with at least two columns is accepted provided that ordination_method = NULL), which creates a two-dimensional scatter plot.

ordination_method

Character, the ordination method that was used to generate the ordination object: "PCA" for Principal Components Analysis (default), "PCoA" for Principal Coordinates Analysis, "NMDS" for Non-metric Multidimensional Scaling, and "LDA" for Linear Discriminant Analysis.

biplot_type

Character, indicating the type of biplot transformation of data: "default" and "pc.biplot", corresponding to the transformations performed in biplot.princomp with pc.biplot = FALSE ("default") and pc.biplot = TRUE ("pc.biplot"). If NULL, no processing is performed, assuming that data within ordination_object was previously transformed.

rows_over_columns

Numeric, the value defining the degree in which distances between observations have priority over distances between variables (0 = observation-focused, 1 = variable-focused). It corresponds to the argument scale in biplot.princomp. It will be ignored if biplot_type = NULL. The default is set at 0.5, which corresponds to a 'SQRT-Biplot', a compromise between the two representations.

groups

A factor variable containing the group assignation of each point.

vips

A list of logical (Boolean) vectors identifying "Very Important Points" under different methods or criteria.

detach_arrows

Logical, wheter to display covariance arrows a independent miniature plot, overlapping the main plot and placed according to arrow_fig.

show_grid

Logical, wheter to display the background grid.

show_axes

Logical, wheter to display the axes.

show_group_legend

Logical, whether to display a legend for groups.

show_vip_legend

Logical, whether to display a legend for vip criteria.

show_arrows

Logical, whether to show variable covariance arrows.

show_fitAnalysis

Logical, whether to display the fit analysis plot corresponding to the ordination method given (Scree plot or Shepard plot).

main_lwd

The line width to be used in the main plot. (lwd in (par).

grid_cex, grid_font, grid_adj

The scale, font type, and text justification of the grid notation.

invert_coordinates

Logical, vector of length two expressing which dimensions, if any, must be inverted before plotting (e.g. for aesthetical reasons).

xlim, ylim

the ranges to be encompassed by the x and y axes, if NULL they are computed (See s.class).

x_title, y_title

Character, texts to be placed in the x and y axes.

x_title_cex, x_title_font

The text parameters of the x axis or horizontal title (par).

y_title_cex, y_title_font

The text parameters of the y axis or vertical title (par).

subtitle

Character, a subtitle to be displayed in the bottom-left corner of the plot (csub in s.class). If equals NULL and ordination_method = "PCA", a default subtitle is the R-Squared of the 2D fit respect to the original distances.

subtitle_position

Character, value indicating the position of the subtitle in the main plot ("topleft", "topright", "bottomleft", and "bottomright"; see s.class).

subtitle_cex

the font size of the subtitle (csub in s.class).

point_type

A character input accepting three values: "point", "label" (displaying the content of point_label) and "point and label", placing both points and labels.

point_pch

A number or a numerical vector given to pch in par.

point_size

The size or scale given to cpoint in s.class.

point_label

A character vector labelling every observation. It's length must be equal to the number of rows in the points/scores of the ordination object (nrow(ordination_object$points) == length(point_label)).

point_label_cex, point_label_font, point_label_adj

The text parameters of the points' labels (text).

point_label_adj_override

A data frame with x,y values to be passed to adj, overrinding the default value given in point_label_adj. The rows must be named exactly as points are referred in the ordination object.

arrow_color

The color or colors to be used in covariance arrows.

arrow_mim_dist

Numeric, the minimum distance of a arrow from the origin of arrows (i.e. zero covariance), in order for it to be displayed (range [0 = all arrows are displayed,1 = no arrow is displayed]).

arrow_length

Numeric, the scalar factor applied to loadings to resize them respect to scores. If detach_arrows = TRUE, resizing is controlled with arrow_fig, so this value is ignored.

arrow_cex, arrow_lwd, arrow_label_cex, arrow_label_color, arrow_label_font, arrow_label_adj

Graphical parameters of the covariance arrows and their labels (see arrows and par). arrow_cex is actually equivalent to length in arrows.

arrow_label_adj_override

A data frame with x,y values to be passed to adj, overrinding the default value given in arrow_label_adj. The rows must be named exactly as variables are referred in the ordination object.

group_color

A vector containing the colors to be used in each group (applied to points, labels, stars and ellipses). If NULL, automatically assign different colors from the rainbow() palette.

group_star_cex, group_ellipse_cex, group_label_cex

The size or scale of the stars, ellipses, and labels representing groups passed to s.class of the ade4 package. Zero values render these elements invisible.

group_ellipse_axes

Logical, wheter to show the ellipses axes, passed to axesell in s.class of the ade4 package.

group_legend_title

Character, the title of the group legend. If equal NULL or "", no title is displayed.

group_legend_title_pos

A numeric vector of length two, the xy position of the title within the group legend box.

group_legend_title_cex, group_legend_title_font, group_legend_title_adj

The text parameters to be applied in the group legend title (par).

group_legend_box_color

The background color of the group legend box.

group_legend_key_pch, group_legend_key_cex, group_legend_key_lwd

The type, size and line width of the keys in the group legend.

group_legend_key_margin

The x position of the keys within the group legend box. Values from 0 to 1.

group_legend_text_margin

The x position of the text entries in the group legend. Values from 0 to 1.

group_legend_text_color

The color or colors of the text entries in the group legend.

group_legend_text_cex, group_legend_text_font, group_legend_text_adj

The text parameters of the text entries in the group legend.

vip_color

The color or colors to be used in vip markings.

vip_pch

A character vector containing the characters used for the vips markings under each criterion.

vip_cex, vip_lwd, vip_font, vip_adj

The graphical parameters of the vips markings.

vip_legend_title

Character, the title of the vips legend.

vip_legend_title_pos

A numeric vector of length two, the xy position of the title within the vips legend box.

vip_legend_title_cex, vip_legend_title_font, vip_legend_title_adj

The text parameters to be applied in the vips legend title (par).

vip_legend_box_color

The background color of the vips legend box.

vip_legend_key_cex

Numeric, the sizing factor of the keys in the vips legend respect to the vips marking in the plot.

vip_legend_key_margin

Numeric, the x position of the keys within the vips legend box. Values from 0 to 1.

vip_legend_text_cex, vip_legend_text_font, vip_legend_text_adj

The text parameters of the text entries in the vips legend.

vip_legend_text_margin

Numeric, the x position of the text entries in the vips legend box. Values from 0 to 1.

fitAnalysis_lwd, fitAnalysis_screePlot_color, fitAnalysis_stress_cex, fitAnalysis_stress_lab_cex, fitAnalysis_stress_axis_cex, fitAnalysis_stress_p_color, fitAnalysis_stress_l_color

The graphical parameters of the plot for fit analysis correspondint to the ordination method (Scree plot for PCA, PCoA, LDA; Shepard or Stress plot for NMDS). (par, stressplot of the vegan package).

test_text

A list of character vectors or expressions with the lines of text presenting the results of statistical tests. A example structure would be: list(c("first line", "second line"), "second paragraph"), bquote("third paragraph" ~ alpha == 2).

test_spacing_paragraph, test_spacing_line

Numeric, relative spacing between paragraphs (list elements) and lines (character elements within a list element, if more than one).

test_cex, test_font, test_adj

The parameters of the text with the test results (par).

fit_into_main

Logical, wheter to fit all elements into the main plot. If TRUE, the 'fig' parameter of every element is interpreted as relative to main_fig.

main_fig, group_legend_fig, vip_legend_fig, arrow_fig, fitAnalysis_fig, test_fig, x_title_fig, y_title_fig

The fig parameter (par) to place in the display region of the graphics device, respectively, the main plot, the group and vip legends, the fit analysis plot, the tests results, and the x and y axes titles.

output_type

A character vector indicating the output image types to be generated. Values accepted are: "png", "tiff", "jpeg", "eps", and "preview" (i.e. R graphics device).

open_new_device

Logical, wheter to build the plot in a new graphics device. If FALSE, the biplot is draw in the current device.

leave_device_open

Logical, wheter to leave the graphics device open, e.g. to continue the plot adding external elements. TRUE is only accepted if output_type has a single value. If output_type = c("preview"), the device is left open by default.

directory

Character, the directory within the working directory. For example, "MyFolder/".

file_name

Character, the name of the output file.

width, height

Numeric, the dimensions of the output image.

family

Character, the font family used in every text in the plot, (par).

Details

This function allows customising virtually every graphical parameter in a 2D biplot, including several extra elements that may be useful for multivariate explorations. It is focused mainly on improving basic visualization aspects of ordination methods through 'classical' biplots. There are several packages that address the creation of other variations of biplot: BiplotGUI, GGEBiplotGUI, multibiplotGUI, biplotbootGUI, NominalLogisticBiplot, OrdinalLogisticBiplot, ade4, vegan, MultBiplotR. When biplot_type = "default", the biplot processing is done as in biplot.princomp, which follows the definition of Gabriel (1971). As in this method, when biplot_type = "pc.biplot", this function creates biplots according with Gabriel and Odoroff (1990). Since there are several types of biplot transformations, it is possible to use 'scores' and 'loadings' that were already transformed, passing biplot_type = NULL. Groups can be represented as stars, ellipsoids, and/or colors, using (s.class), which can be tracked by a fully-customisable legend (group_legend arguments). Individual observations deemed exceptional (vip = Very Important Points) can be marked with custom symbols. Whenever is are more than one type of marking (e.g. different methods/criteria of outlier detection), the different symbols can be presented in a legend (vip_legend arguments). When desired, it is possible to display a Scree plot representing the eigenvalues (Principal Components Analysis, Principal Coordinates Analysis) or a Shepard or stress plot (Nonmetric Multidimensional Scaling, e.g. metaMDS in the vegan package) by enabling show_fit_analysis. It is also possible to display statistical test results by enabling show.tests and introducing the corresponding lines in test_text. The position of all elements (legend boxes, title and subtitle, fit analysis plot and tests) can be customized using the corresponding fig parameter.

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
## Not run: 

# Use iris data
data("iris")

# get an ordination object
# ("PCA" is the default input of this function)
pca <- princomp(iris[, 1:4])

# Default plot using Species as the group factor
biplot_2d(pca, groups = iris$Species)

# Use the typical visualization,
# placing scores and loadings around the same origin
biplot_2d(pca, groups = iris$Species, detach_arrows = FALSE)

# Compare different versions of the classical biplot
# "default" vs. "pc.biplot"
biplot_2d(pca,
          output_type = "preview",
          leave_device_open = TRUE,
          x_title = 'biplot_type = "default"',
          x_title_fig = c(0, 1, 0.9, 1),
          fit_into_main = TRUE,
          main_fig = c(0, 0.499, 0, 1))
biplot_2d(pca,
          output_type = "preview",
          open_new_device = FALSE,
          biplot_type = "pc.biplot",
          x_title = 'biplot_type = "pc.biplot"',
          x_title_fig = c(0, 1, 0.9, 1),
          fit_into_main = TRUE,
          main_fig = c(0.5099, 1, 0, 1))

# varying focus on representing distances
# between observations or between variables
biplot_2d(pca,
output_type = "png",
leave_device_open = TRUE,
rows_over_columns = 1,
x_title = 'observation-focused\nrows_over_columns = 1',
x_title_fig = c(0, 1, 0.9, 1),
fit_into_main = TRUE,
main_fig = c(0, 0.329, 0, 1),
width = 1200)
biplot_2d(pca,
          open_new_device = FALSE,
          leave_device_open = TRUE,
          rows_over_columns = 0.5,
          x_title = 'compromise\nrows_over_columns = 0.5',
          x_title_fig = c(0, 1, 0.9, 1),
          fit_into_main = TRUE,
          main_fig = c(0.331, 0.659, 0, 1))
biplot_2d(pca,
          open_new_device = FALSE,
          rows_over_columns = 0,
          biplot_type = "pc.biplot",
          x_title = 'variable-focused\nrows_over_columns = 0',
          x_title_fig = c(0, 1, 0.9, 1),
          fit_into_main = TRUE,
          main_fig = c(0.661, 1, 0, 1))

# ---------------------------------------------------------
# Plot groups as different colors and point types (pch),
# make group star, ellipsis, and label invisible and
# add a group legend with a a custom title.
biplot_2d(pca,
          groups = iris$Species,
          group_color = NULL,
          point_pch = c(1, 3, 2),
          group_star_cex = 0,
          group_ellipse_cex = 0,
          group_label_cex = 0,
          show_group_legend = T,
          group_legend_title = "Species")

# ---------------------------------------------------------
# Polish covariance arrows
# Abbreviate variables names
dimnames(pca$loadings)[[1]] <- c("SL", "SW", "PL", "PW")
# Set a specific justification (adj) for each variable label
arrow_label_adj_override <- rbind(c(-0.1, 0),
                                  c(-0.1, 0.5),
                                  c(0.5, 1.3),
                                  c(0.5, 1.3))
row.names(arrow_label_adj_override) <-
   dimnames(pca$loadings)[[1]]
# Plot: arrows with different colors and
# without the background grid
biplot_2d(pca,
          groups = iris$Species,
          point_pch = c(1, 3, 2),
          group_star_cex = 0,
          group_ellipse_cex = 0,
          group_label_cex = 0,
          show_group_legend = T,
          group_legend_title = "Species",
          arrow_color = c("orange",
                          "blue",
                          "red",
                          "green"),
          arrow_label_adj_override = arrow_label_adj_override,
          show_grid = FALSE)

# ---------------------------------------------------------
# Get arbitrary Very Important Points
irisVIP <- list(setosa = (1:nrow(iris) == 16 |
                          1:nrow(iris) == 42),
                versicolor=(1:nrow(iris) == 61),
                virginica=(1:nrow(iris) == 107 |
                           1:nrow(iris) == 118 |
                           1:nrow(iris) == 132))

# Plot observations using their names and group by Species using only color.
# Mark the VIP and add the respective legend with custom characters.
biplot_2d(pca,
          groups = iris$Species,
          point_type = "label",
          point_label = row.names(iris),
          group_color = c("red", "blue", "green"),
          group_star_cex = 0,
          group_ellipse_cex = 0,
          group_label_cex = 0,
          show_group_legend = TRUE,
          group_legend_title = "",
          vips = irisVIP,
          vip_pch = c("X", "O", "+"),
          vip_cex = c(2, 2, 3),
          vip_legend_fig = c(0.01, 0.25, 0.7, 0.99),
          show_axes = FALSE)

# ---------------------------------------------------------
# Test the setosa separation
irisDist <- dist(iris[, 1:4])
setosaSeparation <- iris$Species == "setosa"
## multivariate test for the setosa separation
require(vegan)
irisTests <- NULL
irisTests$permanova <- adonis(irisDist~setosaSeparation)
irisTests$permdisp2 <- permutest(betadisper(irisDist,setosaSeparation),
                                 pairwise = TRUE)
# This function prepares a list of character vectors containing the test results
getTestText <- function(tests){
  permanova_F <- as.character(round(tests$permanova$aov.tab$F.Model[1], 3))
  permanova_pvalue <- as.character(round(tests$permanova$aov.tab$"Pr(>F)"[1], 3))
  permanova_rSquared <- as.character(round(tests$permanova$aov.tab$R2[1], 3))
  permdisp2_F <- as.character(round(tests$permdisp2$tab$F[1], 3))
  permdisp2_pvalue <- as.character(round(tests$permdisp2$tab$"Pr(>F)"[1], 3))
  text <- list(c(paste("PERMANOVA:\n   F = ", permanova_F,
                       " (p = ", permanova_pvalue, ")\n", sep = ""),
                 c(expression(paste("   ", R^2, " =", sep = "")),
                   paste("          ", permanova_rSquared, sep = ""))),
               paste("PERMDISP2:\n   F = ", permdisp2_F,
                     " (p = ", permdisp2_pvalue,")", sep = ""))
  return(text)
}

# Plot observations using points and groups as colored stars with no labels.
# Place tests results in the top left corner and give a custom horizontal title.
biplot_2d(pca,
          groups = iris$Species,
          group_color = NULL,
          group_ellipse_cex = 0,
          group_label_cex = 0,
          show_group_legend = TRUE,
          group_legend_title = "",
          test_text = resultText(irisTests),
          test_fig = c(0.01,0.5,0.6,0.95),
          show_axes = FALSE,
          x_title = "testing setosa separation",
          x_title_cex = 1.5)


## End(Not run)

Andros-Spica/biplot2d3d documentation built on June 10, 2020, 1:38 p.m.