使用ggplot2创建动态标题+副标题

时间:2016-10-10 14:47:55

标签: r ggplot2

我使用以下代码创建一个带有title和substitle的ggplot图:

这是用于生成数据帧的代码

t <- c(1.4,2.1,3.4)
time <- c(10,11,12)
df_match <- data.frame(t, time)

这是生成情节的代码。

g <- ggplot(df_match, aes(time, t)) + geom_point()
g + theme(axis.text.x = element_text(angle = 90)) + ggtitle(expression(atop("Head", atop(italic("Location"), "")))) 

这很好用。但是,当我想要创建动态图表时,我会这样做:

title <- "Dynamic"

而且:

g <- ggplot(df_match, aes(time, t)) + geom_point()
g + theme(axis.text.x = element_text(angle = 90)) + ggtitle(expression(atop(title, atop(italic("Location"), "")))) 

我得到&#34;标题&#34;作为标题代替&#34;动态&#34;。对这里出了什么问题的想法?

2 个答案:

答案 0 :(得分:4)

使用bquote评估您需要评估的位数。将您要评估的对象放在.()

g + ggtitle(bquote(atop(.(title), atop(italic("Location"), ""))))

答案 1 :(得分:1)

使用问题中的gtitle,尝试substitute

g + theme(axis.text.x = element_text(angle = 90)) + 
    ggtitle(substitute(atop(title, atop(italic("Location"), "")), list(title = title)))

,并提供:

enter image description here

相关问题