title: How to change the chart title dynamically in R?
body: <p>Here is an example using <code>mtcars</code> to split by a variable into separate plots. What I created is a scatter plot of <code>vs</code> and <code>mpg</code> by splitting the data set by <code>cyl</code>. First an empty list is created. Then I used <code>lapply</code> to loop through the values of cyl (4,6,8) and then <code>filter</code> the data by that value. After that I plotted the scatter plot for the subset and saved it to the empty list.</p>
  <pre><code>library(dplyr)
  library(ggplot2)
  gglist &lt;- list()
  
  gglist &lt;- lapply(c(4,6,8), function(x){
      
      ggplot(filter(mtcars, cyl == x))+
          geom_point(aes(x=vs,y=mpg))+
          labs(title = &quot;Relationship between vs and mpg based on the respective cyl&quot;)
  })
  gglist
  </code></pre>
  <p>The output returns three scatter plots with the title <code>&quot;Relationship between vs and mpg based on the respective cyl&quot;</code>. However I wish to dynamically change the title of each scatter plot based on the unique value of <code>cyl</code>.</p>
  <pre><code>unique(mtcars$cyl)
  #[1] 6 4 8
  </code></pre>
  <p>Expected output of various chart titles is as folllows.</p>
  <pre><code>#&quot;Relationship between vs and mpg when cyl is 4&quot;
  #&quot;Relationship between vs and mpg when cyl is 6&quot;
  #&quot;Relationship between vs and mpg when cyl is 8&quot;
  
   
  </code></pre>