Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

r - Combining `expression()` with ` `

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)

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here

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

enter image description here

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...