title: Automate figure production in R
body: <p>I have a series of csv files called according to a specific format:</p>
  
  <p>4 prefices "matrix_del_cats_", "matrix_add_cats_", "matrix_del_groups_", "matrix_add_groups_", followed by a count of replicates 0 to 9, followed by "_" and 6 vectors "[1, 0, 0, 0, 0, 0]" to "[0, 0, 0, 0, 0, 1]"</p>
  
  <p>It looks likes this (non-exhaustive) list:</p>
  
  <pre><code>matrix_add_cats_0_[1, 0, 0, 0, 0, 0].csv
  matrix_add_cats_1_[1, 0, 0, 0, 0, 0].csv
  matrix_add_cats_2_[1, 0, 0, 0, 0, 0].csv
  ...
  matrix_add_cats_9_[1, 0, 0, 0, 0, 0].csv
  matrix_add_cats_0_[0, 1, 0, 0, 0, 0].csv
  matrix_add_cats_1_[0, 1, 0, 0, 0, 0].csv
  matrix_add_cats_3_[0, 1, 0, 0, 0, 0].csv
  ...
  matrix_add_cats_9_[0, 1, 0, 0, 0, 0].csv
  ...
  matrix_add_cats_0_[0, 0, 1, 0, 0, 0].csv
  ...
  matrix_add_cats_0_[0, 0, 0, 1, 0, 0].csv
  ...
  matrix_add_cats_0_[0, 0, 0, 0, 1, 0].csv
  ...
  matrix_add_cats_0_[0, 0, 0, 0, 0, 1].csv
  ...
  </code></pre>
  
  <p>Each csv file looks like this:</p>
  
  <blockquote>
    <p>Name,A,B,C,D,E,F,G,H,I,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA
    A,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0
    B,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0
    C,0,0,0,0,0,0,0,7,5,7,0,0,0,5,0,0,7,5,0,5,7,5,0,0,0,0,0
    D,5,0,0,5,5,7,0,0,0,4,0,0,0,0,0,0,0,0,0,5,5,5,0,0,0,0,0
    E,0,0,0,5,0,0,0,5,0,5,7,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0
    F,0,0,0,7,0,0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0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,5,7,0
    Y,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,5,5,0,0,5,0,5,0
    Z,0,0,0,0,0,0,0,0,5,5,0,0,5,0,0,0,0,0,0,5,7,0,0,7,5,0,0
    AA,0,0,0,0,0,0,0,5,5,0,5,0,5,7,7,0,0,5,0,5,5,0,0,0,0,0,0</p>
  </blockquote>
  
  <p>I need to automatically:</p>
  
  <ol>
  <li>Read each of these csv files</li>
  <li>to produce a figure with a specific title</li>
  <li>and save it to a png file of the save name than the csv file</li>
  </ol>
  
  <p>The titles should look like this:</p>
  
  <blockquote>
    <p>"Interactions of" + {"groups" OR "categories". The former if "groups"
    appears in the csv file, the latter if "cats" appears in the file
    name} + " according to " + {A, B, C, D, E OR F. With "A" if [1, 0, 0, 0, 0, 0], "B" if [0, 1, 0, 0, 0, 0], etc.}</p>
  </blockquote>
  
  <p>Here's my code for an individual figure:</p>
  
  <pre><code>library(ggplot2)
  install.packages("extrafont");library(extrafont)
  font_import(pattern = 'Akk') 
  library(reshape2)
  
  t1 &lt;- read.csv("/matrix_add_cats_0_[1, 0, 0, 0, 0, 0].csv", check.names = FALSE, sep = ",")
  t2 &lt;- read.csv("/matrix_del_cats_0_[1, 0, 0, 0, 0, 0].csv", check.names = FALSE, sep = ",")
  
  tableau &lt;- cbind(t1[,1, drop=FALSE], t1[,-1] - t2[,-1])
  mylevels &lt;- tableau$Name
  tableau.m &lt;- melt(tableau)
  
  #reorder factors
  tableau.m$Name &lt;- factor(tableau.m$Name,levels=mylevels)
  tableau.m$variable &lt;- factor(tableau.m$variable, levels=mylevels)
  
  p &lt;- ggplot(tableau.m, aes(variable,Name)) + 
        geom_tile(aes(fill = value), colour = "white") + 
        scale_fill_distiller(palette = "YlGnBu",limits=c(min(tableau.m$value), max(tableau.m$value))) +
        geom_text(aes(label=value), family="AkkuratLightPro-Regular", color = "black",lineheight=.5,size = 4)
  
  base_size &lt;- 9
  p + theme_grey(base_size = base_size) + 
    labs(x = "", y = "") + scale_x_discrete(expand = c(0, 0)) + 
    scale_y_discrete(expand = c(0, 0)) + 
    theme(legend.position = "none", axis.ticks = element_blank(),
    axis.text.x = element_text(size = 12, angle = 270, hjust = 0, colour = "grey50", family="AkkuratPro-Regular")
    ,axis.text.y = element_text(size = 12, angle = 0, hjust = 1, colour = "grey50", family="AkkuratPro-Regular")) +
    ggtitle("***") + 
       theme(plot.title = element_text(size = 16, angle = 0, colour = "grey25", family="AkkuratPro-Regular"))
  
  ggsave(file="***.png")
  </code></pre>
  
  <p>Although this is pretty complicated, I'm pretty sure this is something that can be done in R. Any clue on how to proceed?</p>