将expression()与\\\<br/>`结合起来 [英] Combining `expression()` with `\n`

查看:141
本文介绍了将expression()与\\\<br/>`结合起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ggplot,在标签文本中使用表达式(phantom(x)> = 80)来获得一个合适的大于或等于符号。



然而,我还需要立即在(N = ...)下面:

 
labs < - c(0-9 \ n(N = 10),10-29\
(N = 10),30-49\
(N = 10),+
50-64 \\ n(N = 10),65-79 \\ n(N = 10),表达(幻影(x)> = 80))

ggplot .d,aes(x = a,y = y))+ geom_point()+
scale_x_discrete(labels = labs)



如何将表达式()与转义符 \\\

$ b结合$ b
解决方案

由于@otsaw说,在他的回答你刚才的问题 plotmath (和t因此,表达式)不允许换行符。

然而,作为一种破解,您可以使用 atop ≥80出现在(N = 10)之上。但您很快就会发现它与其他标签不匹配:

  labs < -  c(0-9 \\ n(N = 10),10-29 \\ n(N = 10),30-49 \\ n(N = 10),
50-64 \( (n = 10),65-79 \\ n(N = 10),
表达式(顶上(幻影(x)> = 80,(N == 10))))

>



所以,作为进一步的黑客,您可以将其他标签作为表达式传递:

<$表达式(atop(0-9,(N == 10))),表达式(atop(10-29,(N == 10)))
表达式(atop(30-49,(N == 10))),expression(atop(50-64,(N == 10))),
expression(atop(65-79, (N == 10))),表达式(atop(phantom(x)> = 80,(N == 10))))



但是当然你有@otsaw解决方案(使用Unicode),这个解决方案比较简单:

(N = 10),10-29(N = 10),$ 0

  labs < 10),30-49(N = 10),
50-64 \\\
(N = 10),65-79\
(N = 10),
\\\≥ 80 \\\
(N = 10))


I have a ggplot where I have used expression(phantom(x) >=80) in the label text to get a proper greater-than-or-equal symbol.

However I also need to have (N=...) immediately underneath:

require(ggplot2)
.d <- data.frame(a = letters[1:6], y = 1:6)

labs <- c("0-9\n(N=10)","10-29\n(N=10)","30-49\n(N=10)", +
   "50-64\n(N=10)","65-79\n(N=10)", expression(phantom(x) >=80))

ggplot(.d, aes(x=a,y=y)) + geom_point() + 
   scale_x_discrete(labels = labs)

How can I combine the expression() with the escape \n ?

解决方案

As @otsaw said in his answer to your earlier question, plotmath (and therefore expression) doesn't allow linebreaks.
However, as a hack, you can use atop to let ≥80 appears on top of (N=10). But as you will soon see it doesn't match with the other labels:

labs <- c("0-9\n(N=10)","10-29\n(N=10)","30-49\n(N=10)", 
          "50-64\n(N=10)","65-79\n(N=10)", 
          expression(atop(phantom(x) >=80, (N==10))))

So, as a further hack, you can pass the other labels as expressions:

labs <- c(expression(atop(0-9,(N==10))),expression(atop(10-29,(N==10))),
          expression(atop(30-49,(N==10))), expression(atop(50-64,(N==10))),
          expression(atop(65-79,(N==10))), expression(atop(phantom(x) >=80, (N==10))))

But of course you have @otsaw solution (using Unicode) that is considerably less wordy:

labs <- c("0-9\n(N=10)","10-29\n(N=10)","30-49\n(N=10)", 
          "50-64\n(N=10)","65-79\n(N=10)", 
          "\u2265 80\n(N=10)")

这篇关于将expression()与\\\<br/>`结合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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