How to use more than one expression in a row
Asked Answered
S

3

5

I am new to R and I am trying to figure out, how to write something like "This is my plot for (\n)8 <=(less than or equal) x <= 10" in my plot-title.

I tried something like this:

plot(1:10, main="Hey, guys, this is my plot for \n")
mtext(c(expression(8 <= x),expression(x <= 10), side=3)

This gives not exactly what I want but "8 (less or equal) x x (less or equal) 10", and those two expressions are printed in a line below the main title, (which is pretty cool) but in the same place, which is pretty much useless :)

I also tried:

plot(1:10, main=bquote(paste(8 <= x, x <= 10, sep=",")))

What kind of does what I want (except the seperator seems to be completely ignored), but I cant add further text. Output: "8 (less or equal) xx (less or equal) 10".

And when I tried

plot(1:10, main=paste("x",bquote(paste(x <= 8,z <= 10,sep=" ")),sep=" "))

the output irritated me completely: the title was: "xpaste (\n) x x <= 8 (\n) x z <= 10 (\n) x" (the (\n) is a line break, the other characters are exactly what is printed)

So is there a possibility to print expressions with more than one operator?

Strafford answered 7/5, 2014 at 13:25 Comment(0)
P
4
heyTitle <- bquote(atop(
    "Hey, guys, this is my plot for",
    {8 <=x}*phantom()<=10
    ))

plot(1:10, main=heyTitle)

Thanks to this answer for the atop() trick.

EDIT: Here is the figure

EDIT 2: borrowing from another answer by G. Grothendieck to use brackets to fix spacing.

enter image description here

Percolate answered 7/5, 2014 at 13:58 Comment(1)
I thank both of you. Now I just need to decide which solution I will use.Strafford
I
4

Try this:

 plot( 1:10, main = ~ bold(atop("Hey, guys, this is my plot for", {"8" <= x} <= "10")))

On the windows() device it looks like this: screenshot

REVISED spacing and made 2nd line a bit larger to match first line. Added rbatt's atop idea. Further simplification.

Insomnia answered 7/5, 2014 at 14:3 Comment(2)
Nice use of {} to deal with spacing. I'm going to use your technique there to update my answer – hope you don't mind (I previously used 8 <=~x~phantom()<=10, now use {8 <=x}*phantom()<=10Percolate
OK. I will trade you a {} for an atop.Insomnia
L
3

Expressions are a bit goofy, but here is how you can do that

plot(1:10, main="Hey, guys, this is my plot for \n")
mtext(expression(paste(8 <= x,", ",x <= 10)), side=3)

To get a vector of expressions, you just use expression(exp1,exp2,exp3). No need to bother with c() or list(). And then to mix expressions with text, paste() often comes in handy. Without paste to combine them as one expression, they will plot on top of each other at the same location.

Larentia answered 7/5, 2014 at 13:50 Comment(1)
As an addition to MrFlick's answer, if only one x was wanted, which is how I took the question: plot(1:10, main="Hey, guys, this is my plot for \n") mtext(expression(paste(8 <= x,"" <= 10)), side=3)Mons

© 2022 - 2024 — McMap. All rights reserved.