2016-10-10 81 views
0

我使用下面的代码创建的标题和substitle一个ggplot图形动态标题+副标题:创建使用GGPLOT2

这是生成数据帧

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

我得到“标题”,如“动态”的称号代替。对这里出了什么问题有任何想法?

+0

这对我的作品: 'title < - “iris”; ggplot(iris,aes(x = Sepal.Width,y = Sepal.Length))+ geom_point()+ labs(title = title)' – ddiez

+0

谢谢,但我也需要一个副标题...任何想法?> –

+2

如果你安装github的dev版本(或稍等一下它的正式版本),有新的官方(简单)支持字幕:https://blog.rstudio.org/2016/09/30/ggplot2-2-2-0 -coming-soon/ – joran

回答

3

使用bquote来评估您需要评估的位。把你想要评价对象在.()

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

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