title: properly formatting a two-line caption in ggplot2
body: <p>I am working on a custom function that can add two-line captions to plots and I want the caption to be formatted properly no matter what the user might choose to enter (<code>"character"</code> or <code>"expression"</code>). I have created a toy example below to illustrate two problems with the way the function has been currently implemented-</p>
  
  <ol>
  <li>When the caption is not <code>NULL</code>, the two lines are not aligned to the
  right. </li>
  <li>When an expression is entered, the concatenated caption gets completely
  mangled.</li>
  </ol>
  
  <p><strong>EDIT:</strong></p>
  
  <p>In case you have a different solution which achieves the same thing (if the user-provided <code>caption</code> is <code>NULL</code>, then the default one-line expression is printed as a caption, otherwise a two-line expression printed as a caption), I am also open to that.</p>
  
  <p>It is <strong>important</strong> though that the class of the object remains <code>"ggplot"</code> because I would like to make further modifications to the resulting plot using <code>ggplot2</code> functions.</p>
  
  
  
  <pre class="lang-r prettyprint-override"><code># needed libraries
  library(ggplot2)
  
  # custom function to prepare a caption
  caption_maker &lt;- function(caption) {
  
    # if caption is not null then add line separator
    if (!is.null(caption)) {
      caption &lt;- paste(caption, "; \n", sep = "")
    }
  
    # prepare the caption with additional info
    caption &lt;- base::substitute(
      expr =
        paste(
          y,
          "In favor of null: ",
          "log"["e"],
          "(BF"["01"],
          ") = ",
          bf
        ),
      env = base::list(
        y = caption,
        bf = 123
      )
    )
  
    # return the message
    return(caption)
  }
  
  # custom function to add labels to the plot
  plot_maker &lt;-
    function(xlab = NULL,
               ylab = NULL,
               title = NULL,
               caption = NULL) {
      caption.text &lt;- caption_maker(caption = caption)
  
      plot &lt;- ggplot(mtcars, aes(wt, mpg)) + geom_point() +
        ggplot2::labs(
          x = xlab,
          y = ylab,
          title = title,
          caption = caption.text
        )
  
      # return the plot
      return(plot)
    }
  
  # this works just fine
  plot_maker(caption = NULL)
  </code></pre>
  
  <p><img src="https://i.imgur.com/AXSu8M5.png" alt=""></p>
  
  <pre class="lang-r prettyprint-override"><code># this works but the caption is not aligned properly
  plot_maker(caption = "This is mtcars dataset")
  </code></pre>
  
  <p><img src="https://i.imgur.com/06O4EAn.png" alt=""></p>
  
  <pre class="lang-r prettyprint-override"><code># this works but the caption is all mangled
  plot_maker(
    caption =
      expression(paste(italic("Note"), ": This is mtcars dataset"))
  )
  </code></pre>
  
  <p><img src="https://i.imgur.com/ZPJJ90H.png" alt=""></p>
  
  <p>Created on 2018-08-22 by the <a href="https://reprex.tidyverse.org" rel="noreferrer">reprex package</a> (v0.2.0.9000).</p>