Pass multiple variables into ggtitle R

ggplot2, r

Solution

I created some fake data to make the code work, but you can of course adapt it for your real data.

dat=data.frame(x=rnorm(10), y=rnorm(10))

ip_case_index <- paste("IP Only Case Index =",
                       round(mean(rnorm(10)), digits = 2))
oa_case_index <- paste("OA Case Index",round(mean(rnorm(10)),
                                             digits = 2))
sn_case_index <- paste("IP and SNF Only"
                       ,round(mean(rnorm(10)), digits = 2))

ggplot(dat, aes(x,y)) + geom_point() + 
  ggtitle(paste0(ip_case_index,"\n", oa_case_index, "\n", sn_case_index))

Problem

I have made some variables that I would like to pass into `ggtitle`. Here are the variables I made ``` ip_case_index <- paste("IP Only Case Index =", round(mean(mdc5ip$case_index), digits = 2) ) oa_case_index <- paste("OA Case Index",round(mean(edata$Std_Pmt_All_Clm / edata$Pred_Amt_Renormal), digits = 2) ) sn_case_index <- paste("IP and SNF Only" ,round(mean(mdc5sn$case_index), digits = 2) ) ``` I want to do something like ``` ggtitle(ip_case_index, oa_case_index, sn_case_index) ``` Which of course does not return the desired title format. How I would like it to show in the title is this ``` ip_case_index oa_case_index sn_case_index ``` Where each variable is on it's own line of the title. I tried using `\n` to add a new line to no avail, I tried using `atop` which made each successive line smaller thus hard to see as it is treating each variable as a subtitle so Title Subtitle subtitle. I have also tried using multiple `paste()` arguments inside of `ggtitle` by using `c(paste(), paste(), paste())`, which returns the first variable. I have also tried the following: ``` plot.title = c(ip_case_index, oa_case_index, sn_case_index) ggtitle(plot.title) ``` which also only give the first one. So I am a bit confused on how to proceed from here. Any help is much appreciated. Thank you,

Original source