In R, the value of the variable MyVar should be evaluated and appear in the title of a simple R plot p.
MyVar<-0.23
The plot p is called using
plot(p, main = MyTitle)
My plot title needs a subscript and should look like this:
What is the correct statement for MyTitle= ?
I tried dozens of variations using paste, expression, substitute, and bquote. Nothing seems to work...
Any help is highly appreciated!
Thanks a bunch!
Mark
I don't believe you can paste an expression to a variable (paste/print/cat/bquote/etc). As a workaround, you could use the "Fbelow=" expression as the title and use mtext to insert the value of MyVar, e.g.
MyVar<-0.23
plot(mtcars[2:3], main = expression('F'[below]*'='))
mtext(text = MyVar, side = 3, adj = 0.625, padj = -1.75, cex = 1.5)
Obviously this isn't ideal, but unless someone else has a clever way of solving your issue this will at least give you a potential option
Related
I'd like to name my plot such as "Plot 1 (p=0.05)".
Please note that the title is bolded and p value is italicized.
I could do manually after plotting contents and do something like:
text(x1, y, "Plot 1", font=2)
text(x2, y, "(p=0.05)", font=3)
However, it's cumbersome to compute coordinates so I'd like to do something like:
title <- (string concatenation here?)
plot(..., main=title)
I failed to find any help on this matter so leave this question here.
Thanks in advance for your help!
I would suggest using the ?plotmath expressions to do your formatting rather than the font= parameters. For example
plot(c(1,3), c(1,3))
text(2,2,expression(bold("Plot 1") ~ (italic("p=.05"))))
Results in
Note that you can't paste() expressions together as easily as you can strings. People often then try to but variables for the parts in quotes but in order to do that, you need to build an expression using bquote() or substitute(). For example
plotname <- "Plot 1"
pvalue <- paste0("p=", formatC(.04944, digits=2, format="f"))
text(2,2,bquote(bold(.(plotname)) ~ (italic(.(pvalue)))))
It is quite simple. Try adding expressions into main=.
like this:
x<- seq(6:1)
y<-c(5,3,77,6,5,1)
main="title", sub="subtitle"
plot(x,y, main=expression(paste(bold("Plot1"), italic("(p=0.05)"))))
I want to add a label with mathematical notations and a number reference to my plot. I tried the expression function and the paste function, but I failed to include what I want to write in my plot.
Here is a easy version of my question:
plot(NA,xlim = c(0, 5), ylim = c(0,5))
a = 5
At point (1,1), I want to add a label γ=5 on the plot. Here is what I tried:
text(1,1,expression(paste(gamma, "=", a)))
But the plot shows that γ=a.
I am wondering how I can include a number reference in the combination of expression and paste function.
Thank you!
I want to use expression for my ylab= when plotting in R. How do I get characters, using expression using the \mathcal{} style font? I am running Ubuntu and I don't want to use ggplot.
To clarify: part of the ylab will contain mathcal characters and part of it will not (and will be a formula). That's why I want to use expression
It is this symbol which I want:
= \mathcal{E}
One possible solution is to use tikz, which is kind of awesome, here is a much less beautiful example than the one they generate in the help of function tikzDevide::tikz:
require(filehash)
require(tikzDevice)
tikz("sinhplot.tex", width = 8, height = 4,
standAlone = TRUE,
packages = c("\\usepackage{tikz}",
"\\usepackage[active,tightpage,psfixbb]{preview}",
"\\PreviewEnvironment{pgfpicture}",
"\\setlength\\PreviewBorder{0pt}",
"\\usepackage{amssymb}")
plot(sinh, col="steelblue", lwd=2, xlim=c(-3,3), xlab="$\\mathcal E= [-3 , 3]$")
grid()
dev.off()
tools::texi2dvi(sinhplot,pdf=T)
system(paste(getOption('pdfviewer'),'sinhplot.pdf'))
The result looks quite nice!
Another solution is to just give up having beautiful characters in the labels of your figures...
Good day!
I am not that familiar to R so I'd be glad to get a little help.
Assume I have the following minimal example:
test <- c(10,20,40,80,80)
avg <- mean(test)
avg <- format(avg,digits=2)
plot(test, xlab="x", ylab="y", pch = 4)
legend("topleft", legend= c("Average: ", avg))
I'd like to write x̄ instead of "average" - wonder if this is event possible as it's not a regular symbol - merely a combination of two (letter plus overline).
The other thing I'd like to get rid of is the line break after the word "Average (see arrow in graphic below):
There are two issues here. The first is that this is handled using ?plotmath in R. The operator you are looking for is bar(). This is not a function but markup that plotmath understands.
The second is that you need an expression in which avg is converted to its value. You need an expression because that is what plotmath works with. There are several solutions to this problem, but the one I use below is bquote(). You provide it an expression and anything wrapped in .( ) will be converted its value by evaluating the thing inside the .( ).
Here is your code and a suitably modified legend() call:
test <- c(10,20,40,80,80)
avg <- mean(test)
avg <- format(avg,digits=2)
plot(test, xlab="x", ylab="y", pch = 4)
legend("topleft", legend = bquote(bar(x)*":" ~ .(avg)))
Do note that this will insert exactly what is in avg. You may need to do
avg <- round(avg)
or some other formatting fix to get something nice and presentable.
Say, I have a variable rv which has some numerical value. Now, I want to plot the value of this variable on a base plot but preceded by a nicely formatted symbol e.g., r subscript m, using expression. To write on the plot I use mtext.
However, what I get is either the value of the variable, but no nicely formatted symbol (left annotation), or a nicely formatted symbol, but not the value of the variable, but the variable name...
I tried to play around with eval, but didn't get what I wanted. Here is my code:
plot(1:10, rep(10,10), ylim=c(0,12))
rv <- 0.43
#left annotation:
mtext(paste(expression(italic(r[M])), " = ", rv), side = 1, line = -1.5, adj = 0.1)
#right annotation:
mtext(expression(paste(italic(r[M]), " = ", rv)), side = 1, line = -1.5, adj = 0.9)
This is the result:
How do i get both, nice format and value of the variable? Thanks.
btw: I know that I can get it, if I use two times mtext and play around with adj and stuff. But I would really like to get it in one call or without playing around with the position of two annotations.
The bquote function will create an expression and alow substitution of values using .(var) syntax. for your case do something like:
text( 5,1, bquote( italic(r[M]) == .(rv) ) )
Just combine what you have and plot two pieces, joined by using adj:
R> plot(1:10, rep(10,10), ylim=c(0,12))
R> text(2,12, expression(paste(italic(r[M]))), adj=1)
R> text(2,12, paste("=", rv), adj=0)