properly formatting a two-line caption in ggplot2 properly formatting a two-line caption in ggplot2 r r

properly formatting a two-line caption in ggplot2


adapting the answer to the linked question,

library(gridExtra)library(grid)library(ggplot2)element_custom <- function() {  structure(list(), class = c("element_custom", "element_text"))}element_grob.element_custom <- function(element, label="", ...)  {  mytheme <- ttheme_minimal(core = list(fg_params = list(parse=TRUE,                                                          hjust=0, x=0)))  disect <- strsplit(label, "\\n")[[1]]  tg <- tableGrob(as.matrix(disect), theme=mytheme)  tg$vp = viewport(just=1,x=1, width = sum(tg$widths))  tg}heightDetails.gtable <- function(x) sum(x$heights)ggplot(iris, aes(Sepal.Length, Sepal.Width)) +  geom_line() +   labs(x= "italic('Note')*': this is mtcars'\n 'in favour of null: '*log[10](bf['01'])=='123'")+  (theme_grey() %+replace% theme(axis.title.x = element_custom()))


How about this:

# needed librarieslibrary(ggplot2)# custom function to prepare a captioncaption_maker <- function(caption) {  # prepare the caption with additional info  caption <- base::substitute(    atop(y,         paste(           "In favor of null: ",           "log"["e"],           "(BF"["01"],           ") = ",           bf         )),    env = base::list(      bf = 123,      y = caption    )  )  # return the message  return(caption)}# custom function to add labels to the plotplot_maker <-  function(xlab = NULL,           ylab = NULL,           title = NULL,           caption = NULL) {    caption.text <- caption_maker(caption = caption)    plot <- ggplot(mtcars, aes(wt, mpg)) + geom_point() +      ggplot2::labs(        x = xlab,        y = ylab,        title = title,        caption = caption.text)    # return the plot    return(plot)  }plot_maker(caption = NULL)plot_maker(caption = "This is mtcars:")plot_maker(xlab = "x Axis Title",  caption = substitute(paste(italic("Note"), ": This is mtcars dataset")))

I got the atop idea from this question