title: How to store custom ggplot aliases as `list` objects in .R script and source them along required label vectors
body: <p>I want to streamline my work with <code>ggplot2</code> given certain settings I repeat across plots. To this end, I want to:</p>
  <ol>
  <li>set aliases for those <code>ggplot2</code> settings (assigned to individual <code>list</code> objects)</li>
  <li>store them all in an <code>.R</code> script</li>
  <li><code>source()</code> the <code>.R</code> script</li>
  <li>call the aliases when creating a plot with <code>ggplot</code></li>
  </ol>
  <p>However, this workflow fails when I set aliases for ggplot functions that call vectors. For example, <code>scale_x_discrete()</code> has an argument called <code>labels</code>, which takes a character vector of strings. This vector can be set in-place (e.g., <code>scale_x_discrete(labels = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;))</code>) or otherwise call a vector object stored in the environment:</p>
  <pre class="lang-r prettyprint-override"><code>labels_for_x &lt;- c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;)
  
  scale_x_discrete(labels = labels_for_x)
  </code></pre>
  <p>Therefore, my problem happens when I want to set an &quot;alias&quot; for a ggplot function that relies on an additional, separate vector.</p>
  <h2>Demonstrating the problem with example</h2>
  <p><strong>Step 1 -- set alias for <code>scale_x_discrete</code> in a new <code>.R</code> script</strong></p>
  <pre class="lang-r prettyprint-override"><code>library(tidyverse)
  
  mygg_scale_x_relabel &lt;-
    scale_x_discrete() %&gt;%
    list
  </code></pre>
  <p>Works!
  But what if:</p>
  <pre class="lang-r prettyprint-override"><code>mygg_scale_x_relabel &lt;-
    scale_x_discrete(labels = labels_for_barplot) %&gt;%
    list
  </code></pre>
  <blockquote>
  <p>Error in check_breaks_labels(breaks, labels) :    object
  'labels_for_barplot' not found</p>
  </blockquote>
  <p>So I can do:</p>
  <pre class="lang-r prettyprint-override"><code>labels_for_barplot &lt;- c()
  
  mygg_scale_x_relabel &lt;-
    scale_x_discrete(labels = labels_for_barplot) %&gt;%
    list
  </code></pre>
  <p>And this runs OK.</p>
  <hr />
  <p><strong>Step 2 -- open a <em>new</em> <code>.R</code>/<code>.Rmd</code> script and source the previous <code>.R</code> script with alias</strong></p>
  <pre class="lang-r prettyprint-override"><code>source(file = ...)
  </code></pre>
  <hr />
  <p><strong>Step 3 -- in the new script, generate a bar plot</strong></p>
  <pre class="lang-r prettyprint-override"><code>library(ggplot2)
  
  df &lt;- data.frame(trt = c(&quot;col_1&quot;, &quot;col_2&quot;, &quot;col_3&quot;), outcome = c(2.3, 1.9, 3.2))
  
  p &lt;-
    ggplot(df, aes(trt, outcome)) +
    geom_col()
  
  p
  </code></pre>
  <p><a href="https://i.stack.imgur.com/XtfxU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XtfxU.png" alt="orig_lanels" /></a></p>
  <hr />
  <p><strong>Step 4 -- (PROBLEM HAPPENS HERE) -- call the <code>scale_x_discrete</code> alias</strong></p>
  <pre class="lang-r prettyprint-override"><code>labels_for_barplot &lt;- c(col_1 = &quot;new_lab_col_1&quot;,
                          col_2 = &quot;new_lab_col_2&quot;,
                          col_3 = &quot;new_lab_col_3&quot;)
  
  p +
    mygg_scale_x_relabel
  </code></pre>
  <p><a href="https://i.stack.imgur.com/b0vT3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b0vT3.png" alt="no_labels" /></a>
  &lt;----- You can see here that all x labels are now gone! <br> <br> This is because I've set <code>labels_for_barplot &lt;- c()</code> (meaning, an empty vector) in my aliases script. But why this cannot be overridden with an updated vector assignment after script is sourced?</p>