Using subscript and line break at the same time in plot titles in R - 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))) )

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

Remove parenthesis from ggplotly legend

I found this example here which provides a perfect way of getting rid of the extra terms added by ggplotly to the legends.
My problem is that the code in the solution removes everything after a comma , and I have an actual comma is my legends which are in the format of DIC, Spliced
The code is:
for (i in 1:length(myplot$x$data)){
if (!is.null(myplot$x$data[[i]]$name)){
myplot$x$data[[i]]$name = gsub("\\(","",str_split(myplot$x$data[[i]]$name,",")[[1]][1])
}
}
My original plot has this kind of legend:
After applying the code, I loose my Spliced/Uspliced terms in my legends
So plotly always turns the new legend to be (orignal_legend,1)(Weird). Is it always ,1) ? or can be ,2) or any other number. I don't know.
If your original legend is (DIC, Spliced, 1) it will become ((DIC, Spliced, 1), 1) with plotly. So you have to remove the beginning round brackets and ending ,1) from the legend. Try this regex with gsub :
for (i in 1:length(myplot$x$data)){
if (!is.null(myplot$x$data[[i]]$name)){
myplot$x$data[[i]]$name = gsub('^\\(|,\\d+\\)$', '', myplot$x$data[[i]]$name)
}
}
As mentioned earlier I am not sure if it is going to be always ,1) or can be ,2) as well, so to be on the safer side I have used \\d+ which will remove any number at the end of the string.

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 add a space to an object name in R

Piston_Rings<-diameter[1:25,]
I want my quality control graph NOT to have the underscore in the object name.
At the moment there is an underscore (not a hyphen) in that object name. It is possible to construct objects whose names have spaces in them but in order to access them you will then always need to use backticks in order to get the interpreter to understand what you want:
> `Piston Rings` <- list(1,2)
> `Piston Rings`[[1]]
[1] 1
> `Piston Rings`[[2]]
[1] 2
The problem you incur is cluttering up your code, at least relative to obeying the usual conventions in R where a space is a token-ending marker to the parser. Hyphens (at least short-hyphens) are actually minus signs.
If on the other hand you only want to use a modified version of a name that contains an underscore as the title for a graph, then try something like this:
Piston_Rings <- list() # just for testing purposes so there will be an object.
plot( 1:10,10:1, main = sub("_", " ", quote(Piston_Rings)) )
#BondedDust's answer is correct, but (guessing, since you haven't been very specific) a simpler way to get what you want is just to specify xlab or ylab arguments to the plot() function. Let's say you have variables stuff (x) and Piston_Rings (y). If you just
plot(stuff,Piston_Rings)
then the plot will have "Piston_Rings" as the y-axis label. But if you
plot(stuff,Piston_Rings,ylab="Piston Rings")
you'll get the label you want. You can also include lots more information this way:
plot(stuff,Piston_Rings,
xlab="Important stuff (really)",
ylab="Piston Rings (number per segment)")
See ?plot.default for many more options.

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)

Resources