title: Loop functions with multiple variables for ggplot2
body: <p>I want to build several plots from one large database, so that I have one plot for each Text (factor) and for each Measure (the many resulting measures of an eye tracking study). The following is a much simpler example of what I am trying to to:</p>
  
  <p>Let's say this is my dataset</p>
  
  <pre><code>Text &lt;- c(1,1,1,1,2,2,2,2,1,1,1,1,2,2,2,2)
  Position &lt;- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
  Modified &lt;- c(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)
  Line_on_page &lt;- c(1, 1, 1, 1, 2,2,2,2 ,1 ,1,1,1,2,2,2,2)
  IA_FIXATION_DURATION &lt;- c(250.3, 70.82, 400, 120.12, 270, 120.5, 100.54, 212.43, 250.3, 70.82, 320.29, 123.12, 260, 121.5, 100.54, 272.43)
  IA_FIXATION_COUNT &lt;- c(1,0,1,1,3,2,0, 1, 1,0,1,2,3,2,0, 2)
  IA_LABEL &lt;- c("she", "did", "not", "know", "what", "to", "say", "to", "she", "did", "not", "know", "what", "to", "do", "to")
  testDF &lt;- data.frame(Text , Position , Line_on_page, Modified, IA_FIXATION_DURATION, IA_FIXATION_COUNT, IA_LABEL)
  </code></pre>
  
  <p>so I want a heatmap (or another graph) for each Text (1/2/3), and for each measure (IA_FIXATION_DURATION/IA_FIXATION_COUNT)</p>
  
  <pre><code># so first i create my vectors
  
  
  library(stringr)
  library(reshape2)
  library(ggplot2)
  library(ggthemes)
  library(tidyverse)
  
  Text_list &lt;- unique(testDF$Text)
  Measure_list &lt;- testDF %&gt;% dplyr::select_if(is.numeric) %&gt;% colnames() %&gt;% as.vector()
  
  # create graphing function
  Heatmap_FN &lt;- function(testDF, na.rm = TRUE, ...){
  
    # create for loop to produce ggplot2 graphs 
    for (i in seq_along(Text_list)) { 
      for (j in seq_along(Measure_list)) {
  
        # create plot for each text in dataset 
        plots &lt;- ggplot(subset(testDF, testDF$Text==Text_list[i])) +
          geom_tile(aes(x=Position, 
                        y=Line_on_page, 
                        fill = Measure_list[j])) +
          geom_text(aes(x=Position, 
                        y=Line_on_page, 
                        label=IA_LABEL),
                    color = "white", size = 2, family = "sans") +
          scale_fill_viridis_c(option = "C", na.value = "black") +
          scale_y_reverse() +
          facet_grid(Page ~ Modified)+
          theme(legend.position = "bottom") + 
          ggtitle(paste(Text_list[i],j, 'Text \n'))
  
        ggsave(plots, file=paste(Measure_list[j], "_T", Text_list[i], ".pdf", sep = ""), height = 8.27, width = 11.69, units = c("in"))
  
      }
    }
  }
  
  Heatmap_FN(testDF)
  </code></pre>
  
  <p>now, I am pretty sure that the problem lies in the geom_tile "fill" part, where I would like to indicate to the function that I want to use the results variables one by one to produce the plot.</p>
  
  <p>Any ideas on how to fix that?
  Thanks</p>