Home > front end >  How can I print mutliple (81) ggplots from a for loop?
How can I print mutliple (81) ggplots from a for loop?

Time:09-16

I have a large, long dataset like this example:

df <- data.frame("Sample" = c("CM","PB","CM","PB"),"Compound" = c("Hydrogen","Hydrogen","Helium","Helium"), "Value" = c(8,3,3,2))

however I have about 162 rows (81 sample/compound pairs)

I am trying to write a loop that prints individual geom_col() plots of each compound where

x=Sample
y=Value

and there are 81 plots for each compound.

I think I am close with this loop:

I want i in "each compound" etc.

for (i in df$Compound){
  print(ggplot(data = i),
               aes(x=Sample,
                   y=Value)) 
    geom_col()
}

What am I missing from this loop? I have also tried facet_wrap(~Compound) However it looks like 81 is too large and each plot is tiny once made. I am looking for a full size bar graph of each compound.

CodePudding user response:

Two issues with your code:

  • Your aes needs to be combined with ggplot(.) somehow, not as a second argument to print.
  • Your geom_col needs to be added to the ggplot(.) chain, not to print.

I think then that your code should be

for (i in df$Compound){
  print(
    ggplot(data = i)  
      aes(x = Sample, y = Value)  
      geom_col()
  )
}

A known-working example:

for (CYL in unique(mtcars$cyl)) {
  print(
    ggplot(subset(mtcars, cyl == CYL), aes(mpg, disp))  
      geom_point()  
      labs(title = paste("cyl ==", CYL))
  )
}

produces three plots (rapidly).

Note:

  1. If you want a break, consider adding readline("next ...") after your print.
  2. I tend to use gg <- ggplot(..) ... ; print(gg) (instead of print(ggplot(.) ...)) mostly out of habit, but it can provide a little clarity in errors if/when they occur. It's minor and perhaps more technique than anything.

CodePudding user response:

I think you can loop and pull out the selected data set for each index.

for (i in df$Compound){
  print(ggplot(data = df[df$Compound == i,],
               aes(x=Sample,
                   y=Value)) 
    geom_col())
}

(This code also fixes the problems/misplaced parentheses pointed out by @r2evans)

There are a variety of other ways to do this, e.g. split() the data frame by Compound, or something tidyverse-ish, or ...

  • Related