Multiple plots using loops in one plot

Hi,
I was creating plots for pca analysis as follows:

new <- data.frame(days_to_last_follow_up, age_at_index, age_at_diagnosis, ajcc_pathologic_t, ajcc_pathologic_m, ajcc_pathologic_n, gender)

scaled_new<- apply(nowy[,1:3], 2, scale)

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")

Is it possible to create a loop that shows four plots on one page? I tried this way, but it only shows in the R window.

par(mfrow=c(4,1))
for (i in 4:7) { 
  df <- new[,i]
  b <- fviz_pca_biplot(df, geom = "point", habillage = df, repel = "TRUE")
  print(b)
}
par(mfrow=c(1,1))

Thanks for help :smiling_face:

par() is a function that is used in laying out base R plots.
ggplot2 plots are not base R plots and so you will need something else.
There are many tools in this space, I can think of 3 and there are likely more.
you could look into :
gridextra
cowplot
patchwork
perhaps look them up on cran and compare their vignettes / intros and see which one appeals to you most.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.