I want to use the function 'expression' in r to be able to add symbols as '≤'
Example:
plot(1:10,1:10)
legend(3,8, c(expression(""<="test ")))
With this code there will be a space between ≤ and test, I want there to be no space: ≤test, how to accomplish this?
Thanks
You could use unicode symbols (\U2264 for ≤) and do it without the expression?
plot(1:10,1:10)
legend(3,8, "\U2264test") # legend(3,8, "≤test")
Related
Specifying text in a base R plot() with formatting such as italics / bold font / newline usually involves one or more of the following functions:
paste()
expression()
atop()
substitute()
italic()
Is there an intuitive explanation for the differences between these functions and when best to apply them?
What you're referring to is the plotmath syntax.
To start off, let's make it clear that for a plotmath expression to be interpreted as such, you tell R it's an "expression" and that is why you need expression().
So any time you want to use special symbols or formatting, like italic() and atop(), it's actually a part of plotmath and so you need to wrap it in an expression. eg:
plot(0, main = expression(atop(over,italic(under))))
If you've tried out ?italic or ?atop, you've probably noticed it takes you straight to the plotmath manual page, where a bunch of other functions are listed.
What about substitute() ? Well in my previous example, you'll notice I used strings directly to write 'over' and 'under', without putting them within quotes. This is because of the special expression() environment.
So if you need to put whatever is inside a variable in your text (rather than the variable name) then you put your expression inside a substitute() and give it the arguments. eg:
plot(0, main = substitute(atop(oo,italic(under))), list(oo='over2')))
Note that we don't put substitute around the expression block but replace it entirely.
Finally, where does paste() come in all this ? Well, paste is the glue (pun intended) with any text not dealt with by plotmath.
So if you need text before or after math symbols (or formatted text), you paste() things together within the expression (or substitute) environment. eg :
plot(0, main = substitute(paste("b4", atop(oo,italic(under)), aft),
list(oo='over', aft = 'after3')))
As before, if you want to paste the content of a variable, you need substitute.
And Voilà that's most of the plotmath you'll ever need!
For any other symbols, or functions, have look at ?plotmath
I have the following utilsnips script that I use for Vim:
snippet - "assignment"
<-
endsnippet
I use it for R to expand a dash to the assignment operator. I would like to make it so that a space is put both before and after the <- on expansion. However, when I put a space before it in the snippet like <-, it won't expand on hitting Tab. How should I modify the script to have spaces around the operator? Desired result: <-.
You could use r option to include head and trailing spaces around snippets. r will treat snippet as a python regular expression and you should define your snippet within quotes when using this flag.
snippet " -" "assignment" r
<-
endsnippet
Note that there is a space before and after <- in snippet definition.
As a bonus, It's more interesting to define the snippet like the following:
snippet " - " "assignment" rA
<-
endsnippet
A is autoexpansion. so now you dont need to hit tab anymore! just type - and as soon as you type space after - it will expand to <- Automatically.
I've learned to apply bold to a portion of the text used in a plot title using mtext() expression() and paste(). This works great if you specify the strings outright. However, in the project I'm working on now, the portion of text to be bolded needs to be obtained through a call to an element of a vector. However, the characters needed in the call syntax are interpreted by expression() and the call fails.
junk <- c("I'm Special", "You're Special")
plot(0, type="n")
mtext(expression(paste("Do you think ", bold(junk[1]),"today?")),3,2)
mtext(expression(paste("I think ", bold(junk[2]), "today.")),3,1)
Any thoughts on how to approach this? I am trying to avoid specifying the bold text directly.
bquote has a decent interface for this. You just surround the variable you want to substitute with .(). You could also use substitute with expression.
junk <- c("I'm Special", "You're Special")
plot(0, type="n")
mtext(bquote(paste("Do you think ", bold(.(junk[1])),"today?")),3,2)
mtext(bquote(paste("I think ", bold(.(junk[2])), "today.")),3,1)
I would like to have the title for the plot in two lines, but this does not work, why? and how can I make it work?
CVal<-1
SumEpsVal<-2
plot(1:10, main=bquote(paste("C=", .(CVal), " \n ", sum(xi), "=", .(SumEpsVal) )))
This here works:
plot(1:10, main=paste("C=1", "\n", "SumXi=2"))
I guess bquote makes something wrong... (look up ?bquote)
I tried to change environment in bqoute (the where-argument) but I don't know which environment to take.
BTW:
plot(1:10, main=bquote(paste("C=", .(CVal), "bla \n ", sum(xi), "=", .(SumEpsVal) )))
makes something crazy with the "bla".
Personally I would use mtext as already suggested. But if you really want it to be a one-liner, you can "cheat" bquote by using atop:
plot(1:10, main=
bquote(atop(paste("C=",.(CVal)), paste(sum(xi),"=",.(SumEpsVal)))))
It even aligns both lines neatly to the center.
The root issue is that plotmath does not support newlines within the
expressions to be output.
Control characters (e.g. \n) are not interpreted in character strings in plotmath,
unlike normal plotting.
You really need to create and output each line separately.
For example :
Lines <- list(bquote(paste("C=", .(CVal))),
bquote(paste(sum(xi), "=", .(SumEpsVal))))
Now output each line The text in the list is converted to expressions do.call
mtext(do.call(expression, Lines),side=3,line=0:1)
One way to achieve this is to use mtext to add an additional line under the main title as follows:
plot(1:10, main=bquote(paste("C=", .(CVal))))
mtext(bquote(paste(sum(xi), "=", .(SumEpsVal) )),side=3,line=0)
There may be a prettier solution, but perhaps this is enough for your needs.
How would you add a subscript to one particular word of a title in R? For example, suppose the title is "A_2 and B_2." How would you add these two subscripts? I know that expression("A"[2]) and expression("B"[2]) individually add subscripts to these letters.
You do not need paste (or quotes for that matter) at all:
expression( A[2]~and~B[2] )
Test:
plot(1,1, main=expression( A[2]~and~B[2] ) )
The syntactic principle is that tildes (which creates a space) and asterisks (non-space plotmath separator) are used to separate items and that no quotes are needed unless you are using a plotmath function name .... such as wanting the word "paste" or "sqrt" to appear in the displayed version of the expression.
Just paste them together:
expression(paste("A"[2], " and B"[2])