Starting axis label with subscript - r

I am trying to add specific labels to my axes in R. I have read numerous posts indicating to use the expression function and brackets to do this. The label I want to add to my axis starts and ends with a subscript.
plot(1:10, xlab=expression([10]'x'[5])
Starting the label with a subscript results in an error:
Error: unexpected '[' in "plot(1:10, xlab=expression(["
I have attempted to add in empty quotation marks as a form of placeholder, but it doesn't seem to work.
Any help would be appreciated.

You need to separate multiple subscripts with an asterisk (*), and start the expression with an empty string
plot(1:10, xlab=expression(''[10]*'x'[5]))

Related

Subscript inside brackets in r

I want to add brackets [] around an expression in ggplot. In my code I want the brackets around the "O2" expression.
I have tried this code for naming the y axis of my plot. But it does not work, where am I going wrong here?
scale_y_continuous(name = bquote('[O'[2], ']'[crit]~(mg~O[2]/L^-'1'))
I get the error "Error in as.environment(where) : invalid object for 'as.environment'"
Thank you in advance.
I'm not sure why are you using bquote here since you don't seem to be inserting any values, a regular expression or quote should be just fine. Also if you check the ?plotmath help page, you'll see the group() and bgroup() functions where are usefult for surrounding elements in delimiters. You can use
scale_y_continuous(name = expression(group("[", O[2], "]")[crit]~(mg~O[2]/L^-'1')))
This will also with with bquote too if you really want to use that instead.
The bquote can be fixed i.e.
... +
scale_y_continuous(name = bquote("["*O[2]*"]"[crit]~(mg~O[2]/L^-1)))
-testing
plot(1, 1, main = bquote("["*O[2]*"]"[crit]~(mg~O[2]/L^-1)))
-output
I managed to fix it with "expression" and "paste" but I'm still interested to know how I can do this with "bquote"
scale_y_continuous(name = expression(paste("[O"[2], "]"[crit]~(mg~O[2]/L^-'1')))

Unicode subscript in R

I want to write \sigma^2_i using unicode I can get two-thirds of the way there with:
"\u03C3\U00B2"
I can't for the life of me figure out how to add subscript. According to this site where I got the unicode for superscript 2 the correct unicode for subscript i is "\u1D62", but in R this does not print subscript i.
I know that for text in plots you can use expression, but this is not for a plot, so expression does not work.
Any help would be appreciated.
edit: it appears that using cat evaluates unicode in cases when just printing doesn't necessarily evaluate the unicode. I still don't understand why the unicode characters are inconsistent. Per this website the unicode character for subscript k is "\u2096", but cat("\u2096") prints a thick |

Using expression and paste in R to format an ion name with units in parantheses

I want to create a clean label to a graph that has the species abbreviation of an ion (in this case Chloride) followed by the concentration units (micro equivalents per liter) enclosed in parentheses. As written, the code mostly produces this, but superscripts the parentheses/units section. Probably missing something small. Using this code snippet with the ylab() command in ggplot2 as a label. Thanks.
My code so far:
cl.label = expression(paste(Cl^- ~(mu~eq ~L^-1)), parse=TRUE)
In the expression, - is an operator so it needs something to "negate." You can give it a phantom object like
cl.label = expression(Cl^-phantom() ~(mu~eq ~L^-1))
or you can treat the - as a literal dash value with
cl.label = expression(Cl^"-" ~(mu~eq ~L^-1))

Custom pch in plot only respects first letter in string

I want the pch in the below to show "B -" however it's only showing "B". How do I remedy this?
plot(1:3,pch="B -",xlab="",ylab="")
Help page of function par and argument pch= says that "Either an integer specifying a symbol or a single character to be used as the default in plotting points". So only first letter is used as symbol.
You could use function text() instead to get all symbols.
plot(1:3,type="n",xlab="",ylab="")
text(1:3,"B -")

Using subscript and line break at the same time in plot titles in R

I wish to include a subscript and a title running into two lines at the same time but am not getting desired result with following commands:
base<-'B1'
compare<-'A1'
plot (1, 1, main = bquote('Annual mean' ~CO[2] ~'Flux Difference: \n' ~.(compare)~ 'minus'~.(base)))
I wish to insert a line break after 'Flux Difference' but its not happening instead this term is hanging nowhere. Please help me.
Thanks,
Munish
This is a fairly common request and there is no really clean answer. Plotmath will not accept a "\n" (and the help page documents this.) One dodge is to use each line as an argument to the plotmath function atop. The other plotmath trick sometimes of use is phantom with an empty argument list to allow the tilde not to throw an error, but that is not needed here:
plot (1, 1, main = bquote( atop('Annual mean' ~CO[2] ~'Flux Difference:' ,
.(compare)~ 'minus'~.(base))) )

Resources