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

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

问题描述

另一张出版物的图表,另一天在ggplot2中调整了一些东西......我在哼哼?我不确定... ...

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

我试图找出如何在轴标签中使用下标字母。第一个例子只是一个数字,只要你在方括号中有一个字符就会失败。 Blah [下标(1d)]基本上是我需要的,但我无法弄清楚如何让它在下标中有字母。尝试过包括paste()等在内的变体。

当然,要加入挫败感......

labs(y =表达式(Blah [12])) - 此作品

labs (y =表达式(Blah [d])) - 这可以工作 d1])) - 这可以工作



labs(y = expression(Blah [1d])) - 这个失败。



想法?

解决方案

我可以想到两个比使用 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()+
实验室(y =表达式(Blah [1d]))

R中的令牌(或名称或符号)不应以数字开头。所以,通过引用或通过非空格分隔符分隔 1 d 来解决该限制, code> * 运算符。将纯数字文字与合法的R符号或标记加入或结合。



获取未定义的百分号:

  ggplot(dat,aes(x = x,y = y))+ 
geom_point()+
labs(y =表达式(Blah [1 * d] *%))

要在pct-sign :

 表达式(Blah [1 * d] *(%))

在R解析中,字符具有特殊含义,因为它表示用户的开始定义的中缀运算符。因此,要将其用作文字,需要引用它。


Another graph for publication, another day gone tweaking things in ggplot2... Am I whinging? I'm not sure...

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

I'm trying to work out how to have subscript letters in an axis label. 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. Have tried variations including paste() etc.

Of course, to add to the frustration...

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.

Thoughts?

解决方案

I can think of two better ways 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"]))

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]*"%"))

To put parens around the pct-sign:

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

The % character has special meaning in R parsing, since it signifies the beginning of a user defined infix operator. So to use it as a literal, it needs to be quoted.

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

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