标题是变量名称时如何将标题添加到ggplot? [英] How to add a title to a ggplot when the title is a variable name?

查看:201
本文介绍了标题是变量名称时如何将标题添加到ggplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  + opts(title = expression(Chart chart_title .. 。))

但这不是:

  chart_title =foo
+ opts(title = expression(chart_title))

也不是:

  chart_title =foo
+ opts(title = chart_title)

标题是变量名称时,如何向ggplot添加标题? / p>

解决方案

正如其他人指出的,您的示例对于变量chart_title是字符串或表达式的情况似乎正常工作。有时候构造标题变量是非常棘手的;例如,如果chart_title使用一些其他变量,并且如果另外使用了一些希腊字符,则简单的 paste(...)不会引起混淆。足够了。要构建这样的标题,你可以使用类似下面的内容:

  foo < -  rnorm(100)$ b $ (粘贴(图号,数字,:,阿尔法,vs,beta,sep =),列表(数字=数字) chart_title< )
qplot(foo,foo)+ opts(title = chart_title)

在构建标题 bquote()时会派上用场。程序化标题建设可能是一个混乱的业务; R FAQ 7.13( http://cran.r-project.org/doc/FAQ/R-FAQ.html )可以让你开始,但即使这个FAQ基本上告诉你在有疑问时搜索R-Help。

At the end of a ggplot, this works fine:

+ opts(title = expression("Chart chart_title..."))

But this does not:

chart_title = "foo"
+ opts(title = expression(chart_title))

nor this:

chart_title = "foo"
+ opts(title = chart_title)

How can I add a title to a ggplot when the title is a variable name?

解决方案

As others pointed out, your example seems to work fine for the cases where the variable chart_title is a string or an expression. Sometimes it's tricky to construct the title variable; for instance, a confusing scenario might arise if chart_title uses some other variables, and if in addition you are using some greek characters so a simple paste(...) doesn't suffice. To construct a title like that, you could use something like the following:

foo <- rnorm(100)
number <- 1
chart_title <- substitute(paste("Chart no. ",number,": ",alpha," vs ",beta,sep=""), list(number = number))
qplot(foo,foo) + opts(title = chart_title)

Another function that comes in handy when constructing titles is bquote(). Programmatic title construction can be a messy business; R FAQ 7.13 (http://cran.r-project.org/doc/FAQ/R-FAQ.html) can get you started, but even that FAQ basically tells you to search R-Help when in doubt.

这篇关于标题是变量名称时如何将标题添加到ggplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆