将多个变量传递给ggtitle R.

时间:2014-05-27 19:01:36

标签: r ggplot2

我已经提出了一些我希望传递给ggtitle的变量。以下是我所做的变量

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

我想做点什么

ggtitle(ip_case_index, oa_case_index, sn_case_index)

当然,这不会返回所需的标题格式。我希望它在标题中显示的是这个

ip_case_index
oa_case_index
sn_case_index

每个变量都在其自己的标题行上。我尝试使用\n添加新行无济于事,我尝试使用atop使每个连续的行变小,因此难以看到,因为它将每个变量视为副标题,因此标题字幕副标题。

我还尝试使用paste()ggtitle内部使用多个c(paste(), paste(), paste())参数,后者返回第一个变量。

我也尝试了以下内容:

plot.title = c(ip_case_index, oa_case_index, sn_case_index)
ggtitle(plot.title)

也只提供第一个。

所以我对如何从这里开始感到困惑。

非常感谢任何帮助。 谢谢,

2 个答案:

答案 0 :(得分:9)

我创建了一些假数据以使代码正常工作,但您当然可以根据实际数据进行调整。

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

答案 1 :(得分:0)

这是使用paste()连接测试字符串和变量的更直接的方法。 ggtitle(paste(“国家(占死亡总数的百分比)”,latestDAy,字幕= paste(“ Duration =”,totalDays,“ Days”))

相关问题