Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass multiple variables into ggtitle R

Tags:

r

ggplot2

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,

like image 413
MCP_infiltrator Avatar asked May 27 '14 19:05

MCP_infiltrator


2 Answers

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))

enter image description here

like image 140
eipi10 Avatar answered Nov 03 '22 10:11

eipi10


This works for me: library(glue) +

labs (title = glue("IP and SNF Only ="
                       , round(mean(rnorm(10)), digits = 2)),
          subtitle = glue("OA Case Index =", round(mean(rnorm(10)),
                                                 digits = 2),
                          "\nIP and SNF Only =", round(mean(rnorm(10)), digits = 2)),
caption = "Some text")
like image 1
glensbo Avatar answered Nov 03 '22 11:11

glensbo