Negatyp
Negatyp

Reputation: 45

How to combine multiple fviz_pca_biplot into one plot

i tried to combine multiple fviz_pca_biplot (factoextra package) plots into one plot using par(mfrow=c(2,2)) is there any way to create one plot from 4 plots?

new.pca <- prcomp(scaled_new, center=TRUE, scale.=TRUE)

new.k <- new[,4]
fviz_pca_biplot(new.pca, geom = "point", habillage = new.k, repel = "TRUE")

new.k1 <- new[,5]
fviz_pca_biplot(new.pca, geom = "point", habillage = new.k1, repel = "TRUE")

new.k2 <- new[,6]
fviz_pca_biplot(new.pca, geom = "point", habillage = new.k2, repel = "TRUE")

new.k3 <- new[,7]
fviz_pca_biplot(new.pca, geom = "point", habillage = new.k3, repel = "TRUE")

Upvotes: 1

Views: 800

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

fviz_pca_biplot produces a ggplot object, which is drawn with the grid graphics system rather than the base R Graphics system. ~par` has no effect on grid graphics.

If you want to arrange multiple ggplots on a single page, it's hard to beat the patchwork package. Just store each biplot then use patchwork to arrange them together.

Obviously, we don't have your data, so I have made some random example plots to demonstrate.

C <- chol(S <- toeplitz(.9 ^ (0:31))) # Cov.matrix and its root
set.seed(17)
X <- matrix(rnorm(32000), 1000, 32)
Z <- X %*% C
new.pca <- prcomp(Z, center=TRUE, scale.=TRUE)

p1 <- fviz_pca_biplot(new.pca, geom = "point", habillage = 'none', repel = TRUE)
p2 <- fviz_pca_biplot(new.pca, geom = "point", habillage = 'none', repel = TRUE)
p3 <- fviz_pca_biplot(new.pca, geom = "point", habillage = 'none', repel = TRUE)
p4 <- fviz_pca_biplot(new.pca, geom = "point", habillage = 'none', repel = TRUE)

library(patchwork)

(p1 + p2) / (p3 + p4)

enter image description here

Upvotes: 1

Related Questions