结合 `expression()` 和 ` ` [英] Combining `expression()` with ` `
问题描述
我有一个 ggplot,我在标签文本中使用了 expression(phantom(x) >=80)
来获得正确的大于或等于符号.
但是我还需要在下面有 (N=...) :
require(ggplot2).d <- data.frame(a = 字母 [1:6], y = 1:6)实验室 <- c("0-9
(N=10)","10-29
(N=10)","30-49
(N=10)", +"50-64
(N=10)","65-79
(N=10)",表达式(phantom(x) >=80))ggplot(.d, aes(x=a,y=y)) + geom_point() +scale_x_discrete(标签=实验室)
如何将 expression()
与转义符
结合起来?
正如@otsaw 在他的对您之前问题的回答中所说的
a>、plotmath
(因此expression
)不允许换行.
但是,作为一个hack,您可以使用atop
让≥80
出现在(N=10)
之上.但是您很快就会看到它与其他标签不匹配:
labs <- c("0-9
(N=10)","10-29
(N=10)","30-49
(N=10)","50-64
(N=10)","65-79
(N=10)",表达式(atop(phantom(x) >=80, (N==10))))
因此,作为进一步的技巧,您可以将其他标签作为表达式传递:
labs <- c(expression(atop(0-9,(N==10))),expression(atop(10-29,(N==10))),表达式(atop(30-49,(N==10))), 表达式(atop(50-64,(N==10))),表达式(atop(65-79,(N==10))), 表达式(atop(phantom(x) >=80, (N==10))))
当然,你有@otsaw 解决方案(使用 Unicode),它的内容要少得多:
labs <- c("0-9
(N=10)","10-29
(N=10)","30-49
(N=10)","50-64
(N=10)","65-79
(N=10)","u2265 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=10)","10-29
(N=10)","30-49
(N=10)", +
"50-64
(N=10)","65-79
(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
?
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=10)","10-29
(N=10)","30-49
(N=10)",
"50-64
(N=10)","65-79
(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=10)","10-29
(N=10)","30-49
(N=10)",
"50-64
(N=10)","65-79
(N=10)",
"u2265 80
(N=10)")
这篇关于结合 `expression()` 和 ` `的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!