Learn R Programming

Correlplot (version 1.1.3)

ggbplot: Create a biplot with ggplot2

Description

Function ggbiplot creates a biplot of a matrix with ggplot2 graphics.

Usage

ggbplot(A, B, main = "", circle = TRUE, xlab = "", ylab = "", main.size = 8,
xlim = c(-1, 1), ylim = c(-1, 1), rowcolor = "red", rowch = 1, colcolor = "blue",
colch = 1, rowarrow = FALSE, colarrow = TRUE, linewidth = 0.25, size = 1.5,
onedimensional = FALSE)

Value

A ggplot2 object

Arguments

A

A dataframe with coordinates and names for the biplot row markers.

B

A dataframe with coordinates and names for the biplot column markers.

main

A title for the biplot.

circle

Draw a unit circle (circle=TRUE) or not (circle=FALSE).

xlab

The label for the x axis.

ylab

The label for the y axis.

main.size

Size of the main title.

xlim

Limits for the horizontal axis.

ylim

Limits for the vertical axis.

rowcolor

Color used for the row markers.

rowch

Symbol used for the row markers.

colcolor

Color used for the column markers.

colch

Symbol used for the column markers.

rowarrow

Draw arrows from the origin to the row markers (rowarrow=TRUE) or not.

colarrow

Draw arrows from the origin to the column markers (colarrow=TRUE) or not.

linewidth

Width of the vectors in the biplot.

size

Size of the labels in the plot.

onedimensional

With onedimensional=TRUE a one dimensional biplot will be created that separates the biplot vectors in the second dimension.

Author

Jan Graffelman (jan.graffelman@upc.edu)

Details

Dataframes A and B must consists of dataframes with three columns labeled "PA1", "PA2" (coordinates of the first and second principal axis) and a column "strings" with the labels for the coordinates. Optionally, these dataframes can contain two columns with labels "ve" and "ho" containing the vertical and horizontal adjustments for the label positions of the variables in the biplot.

Dataframe B is optional. If it is not specified, a biplot with a single set of markers is constructed, for which the row settings must be specified.

References

Graffelman, J. and De Leeuw, J. (2023) Improved approximation and visualization of the correlation matrix. The American Statistician 77(4): 432-442. tools:::Rd_expr_doi("10.1080/00031305.2023.2186952")

See Also

bplot,ggtally,biplot

Examples

Run this code
data("HeartAttack")
X <- as.matrix(HeartAttack[,1:7])
n <- nrow(X)
Xt <- scale(X)/sqrt(n-1)
res.svd <- svd(Xt)
Fs <- sqrt(n)*res.svd$u # standardized principal components
Gp <- crossprod(t(res.svd$v),diag(res.svd$d)) # biplot coordinates for variables
rows.df <- data.frame(Fs[,1:2],as.character(1:n))
colnames(rows.df) <- c("PA1","PA2","strings")
cols.df <- data.frame(Gp[,1:2],colnames(X))
colnames(cols.df) <- c("PA1","PA2","strings")
ggbplot(rows.df,cols.df,xlab="PA1",ylab="PA2",main="PCA")

Run the code above in your browser using DataLab