0

I am trying to write a for loop that creates multiple plots, with the dots coloured based on columns in a dataframe:

variables <- colnames(df)[4:35]

for(x in variables){
  Y<- df %>% 
    ggplot(aes(x = PC1, y = PC2, 
               colour = x)) +
    geom_point() + 
    theme_bw(base_size=16) +
    labs(x=paste0("PC1: ",round(var_explained[1]*100,1),"%"),
         y=paste0("PC2: ",round(var_explained[2]*100,1),"%"))

gridExtra::grid.arrange(y)
}

this does not do as I like, at first it does not color the dots, and secondly does not return a grid with all plots.

szmple
  • 453
  • 1
  • 8
  • It looks like you are using Y upper case for the plot but then passing y lower case to grid.arrange. Second, aes() uses non standard evaluation. So you have passed a character vector x to aes() but it wants the unquoted names. Like PC1. So you can wrap in {{ }}. See https://stackoverflow.com/questions/64065003/how-do-double-curly-brackets-work-in-dplyr – alice m w Sep 30 '22 at 11:06
  • @alicemw making it {{x}} still does not resolve the issue i am having. – szmple Sep 30 '22 at 12:27

0 Answers0