title: Loop violin plots of controls with clinical case data points overlaid (ggplot)
body: <p>New to posting on here. Apologies if I miss including something needed to solve my situation.</p>
  <p>I have a matched case-control design where three 'younger' clinical cases have been age-matched to a 'younger' control group, and three 'older' cases have been matched to an 'older' control group. I am plotting the control group distribution in a violin plot and overlaying the corresponding matched cases as  data points.</p>
  <p>I have a lot of variables and I would like to loop through them to minimise error and increase efficiency. I have had a go at writing the code for the loop but I am not sure what to do with the fact that I have two types of plots (violin and point) and two data frames (controls and cases) involved.</p>
  <p><strong>Here is the code I have for the plots:</strong></p>
  <p>#fake data</p>
  <pre><code>cases &lt;- data.frame(
    id = factor(1:6),
    strange_stories_ToM_mean = sample(6:8, 6, replace = TRUE),
    age = factor(c(rep(&quot;young&quot;, 3), rep(&quot;old&quot;, 3)))
  )
  controls &lt;- data.frame(
    id = 7:23,
    strange_stories_ToM_mean = sample(c(6,6,7,7,7,7,7,7,7,8,8,8,9,9,9,9,9), 17),
    age = c(rep(&quot;young&quot;, 9), rep(&quot;old&quot;, 8))
  )
  </code></pre>
  <p>#plots</p>
  <pre><code>ggplot(data = controls, aes(strange_stories_ToM_mean, age)) + 
    geom_violin(
      trim = FALSE, 
      alpha = 0.2, 
      draw_quantiles = c(0.25, 0.5, 0.75), 
      fill = &quot;gray90&quot;
    ) +
    geom_point(
      data = cases, 
      aes(colour = id, shape = id),  # map color/shape to individual cases
      size = 5,
      show.legend = FALSE
    ) +
    scale_shape_manual(values=c(16, 17, 15, 16, 17, 15)) +
    scale_colour_manual(values=c(&quot;deeppink1&quot;,&quot;indianred3&quot;, &quot;blueviolet&quot;, &quot;springgreen3&quot;, &quot;chartreuse2&quot;, &quot;darkgreen&quot;)) +
    scale_size_manual(values=c(5, 4, 5, 5, 4, 5)) +
    theme_classic() 
  
  ggsave(&quot;strange_stories_ToM_mean.svg&quot;, width = 8, height = 8, units = &quot;cm&quot;)
  </code></pre>
  <p>I looked at using 'for' and created a list to loop through (what I have is below) but I came unstuck at where the list should be incorporated when two data frames are being used and two plots...could lapply be best?</p>
  <pre><code>variables &lt;- list()                   # Create empty listfor(i in ncol(FTD_data)) {            # Using for-loop to add all columns tolist variables[[i]] &lt;- FTD_data[ , i]}
  
  names(variables) &lt;- colnames(FTD_data) #rename list elements with variable names from df
  
  for (i in variables)
  {CODE TO PLOT INSERT HERE}
  </code></pre>