Subscript inside brackets in r - 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')))

Related

How to place an expression on top of another expression using plotmath

If I want to generate a multi-line expression I can do:
over = 'OVER'
below = 'BELOW'
x_lab_title = bquote(atop(.(over), .(below)))
ggplot(data.frame()) + xlab(x_lab_title)
However, I would like to make over and below expressions on their own, e.g.:
over = expression(X^2)
below = expression(Y^2)
but it does not work - renders a blank space instead (why is that?). In my scenario, the real expression is much more complicated, autogenerated, and changing between subsequent plots I generate, thus I cannot simply do:
x_lab_title = bquote(X^2, Y^2)
which would work in this particular case, but not for any other value of over and below.
The problem is that an expression() in R actually acts more like a container of expressions. What you really want is the language object inside the expression object. So you could either do
over = expression(X^2)
below = expression(Y^2)
x_lab_title = bquote(atop(.(over[[1]]), .(below[[1]])))
or, even better, skip the expression() and just use quote()
over = quote(X^2)
below = quote(Y^2)
x_lab_title = bquote(atop(.(over), .(below)))
After some hours of trial and error found a solution - this worked for me:
atop_string = paste0('bquote(atop(', toString(over), ',', toString(below), '))')
x_lab_title = eval(parse(text=atop_string))
This solution first assembles equivalent code representation and then parses it into an expression.
Even though I found a solution on my own, I am still curious why I cannot simply embed expressions in atop?

Starting axis label with subscript

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

How to make the font bold in R's bquote for main of plot?

I make some plots with R and use bquote because I need variables for the main of the plot. However, the main is no longer bold but I want it to be bold. I defined the main as follows:
title = bquote(atop("Empirical Pricing Kernel at Date",~.(EndDate)~"with Index Price"~.(ST)~"€"))
plot(temp, EPK, type="l", main = title)
Enddate contains "2014-08-01" as date and ST is just numeric with 9210.08.
Is there any way to make it bold with or without bquote? I'd like to find a solution with bquote because it's very convenient when using subscripts.
My problem is that I am using it in a par-plot with two plots and the other plot needs no special things in it's main. So, the main is bold. I even tried to just put bquote around it in order to get the same font size but it stayed bold.
I prefer to use what I think of as "pure plotmath" so I use tilde's instead of spaces and use no quotes. I suspect it was the leading tilde in the second argument to bquote that was throwing the error. In plotmath the tildes need something on either side: If you really need a none-displayed something you can always use phantom(0) but why bother in this case?
bquote(atop(Empirical~Pricing~Kernel~at~Date,
bold(.(EndDate))~with~Index~Price~.(ST)~"€"
) )
Test:
EndDate="2014-08-01";ST=9210.08
title = bquote(atop(Empirical~Pricing~Kernel~at~Date, bold(.(EndDate))~with~Index~Price~.(ST)~"€"))
plot(1,1, type="l", main = title)

In R, how to horizontally align strings and math expressions appearing on separate rows in plot titles [duplicate]

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.

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