ggplot轴标签中的下标字母 [英] Subscript letters in ggplot axis label

查看:24
本文介绍了ggplot轴标签中的下标字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究如何在轴标签中添加下标字母.

I'm trying to work out how to have subscript letters in an axis label.

dat <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(dat, aes(x=x,y=y)) +
    geom_point() +
    labs(y=expression(Blah[1]))

dat <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(dat, aes(x=x,y=y)) +
    geom_point() +
    labs(y=expression(Blah[1d]))

第一个示例有效,因为它只是一个数字,只要方括号中有一个字符,它就会失败.Blah[subscript(1d)] 本质上是我需要的,但我不知道如何让它让我在下标中有字母.我尝试过各种变体,包括 paste().

The first example works as it's just a number, as soon as you have a character in the square brackets, it fails. Blah[subscript(1d)] is essentially what I need, but I can't work out how to get it to let me have letters in subscript. I have tried variations, including paste().

以下示例提供了奇怪的行为:

The following examples provide strange behavior:

labs(y=expression(Blah[12])) # this works
labs(y=expression(Blah[d])) # this works
labs(y=expression(Blah[d1])) # this works
labs(y=expression(Blah[1d])) # this fails

想法?

推荐答案

最后一个失败的原因是 expression 的参数通过 R 解析器运行,失败时返回错误测试它们是否可能是正确的 R 语法.字符串或标记 1d 不是有效的 R 标记(或符号).有可能将其分解为有效的 R 令牌并连接"它.使用非空格运算符,反引号 it 或使用普通引号.我认为这比使用 paste 更好:

The reason the last one fails is that the arguments to expression get run through the R parser and an error is returned when they fail the test of whether they could possibly be correct R syntax. The string or token 1d is not a valid R token (or symbol). It would be possible to either break it into valid R tokens and "connect" with non-space operators, backtick it , or use ordinary quotes. I think either is a better way than using paste:

 ggplot(dat, aes(x=x,y=y)) +
     geom_point() +
     labs(y=expression(Blah[1*d]))
 ggplot(dat, aes(x=x,y=y)) +
     geom_point() +
     labs(y=expression(Blah["1d"]))

R 中的标记(或名称"或符号")不应以数字开头.因此,您可以通过引用或将 1d 用非空格分隔符(* 运算符)分隔来绕过该限制.那个加入"或连接物"带有合法 R 符号或标记的纯数字文字.

Tokens (or "names" or "symbols") in R are not supposed to start with digits. So you get around that limitation by either quoting or by separating 1 and d by a non-space separator, the * operator. That "joins" or "ligates" a pure numeric literal with a legal R symbol or token.

要获得无下标的百分号:

To get a percent sign unsubscripted just:

 ggplot(dat, aes(x=x,y=y)) +
    geom_point() +
    labs(y=expression(Blah[1*d]*"%"))

在 pct 符号周围放置括号:

To put parens around the pct-sign:

expression(Blah[1*d]*"(%)")

% 字符在 R 解析中具有特殊意义,因为它表示用户定义的中缀运算符的开始.因此,将其用作文字需要引用它.同样的推理要求for"和在"被引用,因为它们在保留词"中R 的组.还有其他保留字,(但 forin 是最常让我失望的词.)输入:

The % character has special meaning in R parsing, since it signifies the beginning of a user defined infix operator. So using it as a literal requires that it be quoted. The same reasoning requires that "for" and "in" be quoted, because they are in the "reserved words" group for R. There are other reserved words, (but for and in are the ones that trip me up most often.) Type:

 ?Reserved

另一个技巧"如果需要斜体,则在 italic() 中的数字周围使用引号.未加引号的数字在该函数内不会变成斜体.

And another "trick" is to use quotation marks around digits within italic()if you need them italicized. Unquoted digits do not get italicized inside that function.

注意事项:paste 是一个 plotmath 函数除了它与 base::paste 函数 的语义不同.特别是,它没有sep"参数.因此,您永远无法在打印的参数之间获得空格,并且如果您尝试放入非空格项,则在所有其他标记为 sep=" 的参数之后将出现一个实例.".

Caveats: paste is a plotmath function except it has different semantics than the base::paste function. In particular, it has no 'sep' argument. So you can never get a space between the printed arguments and if you try to put in a non-space item, a single instance will appear after all the other arguments labeled as sep=" ".

paste0 不是绘图数学函数,因此不会被解释,而是会显示为未处理";其未处理的参数在括号内.

paste0 is not a plotmath function and so will not get interpreted but rather will appear "unprocessed" with its unprocessed arguments inside parentheses.

这篇关于ggplot轴标签中的下标字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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