I have a little trouble to add the equations into scatter plots using "legend"
A simple example is as follow:
plot(1:100)
# The below code can work if I add "= 0.1234" directly.
legend(locator(1), expression(paste("Linear model: ", R^2, "= 0.1234",sep="")),
text.col= "black",cex=1,bty="n")
# The below code cannot work if I add the "ps".
ps = "= 0.1234"
legend(locator(1), expression(paste("Linear model: ", R^2, ps, sep="")),
text.col= "red",cex=1,bty="n")
The real issue I have is a little complex with this example.
So how should I revise this code?
The "ps"-object is being handled as an expression, i.e. not being evaluated. To get around this use bquote and .()
legend(locator(1), legend= bquote("Linear model: "* R^2*.(ps)),
text.col= "red",cex=1,bty="n")
BTW the first version would be more compactly represented without the paste:
legend(locator(1), expression(Linear~model*":"~ R^2 == 0.1234),
text.col= "black",cex=1,bty="n")
The only thing needing to be quoted is the semi-colon.
Related
I'm plotting photosynthetically active radiation which has units of umol/m2/s
I can get R to plot "PAR umol m-2" with the u as mu and -2 in superscript using:
bp_m + ylab(expression(paste('PAR ',mu,'mol m')^-2))
where bpm is the rest of the plot, but I can't get it to add the s^-1, I've tried various forms of expressions, paste etc
Finally got there - R seems to have so many different ways of doing the same thing, taking me ages to get my head around the syntax etc
bp_m+labs(y = ~paste("PAR ", mu, "mol m"^-2,"s"^-1))
bp_m+ylab(~paste("PAR ", mu, "mol m"^-2,"s"^-1)))
both work
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 write a R-squared term for my legend but I do not know how. Could someone help me please?
My legend syntax is:
legend(2,10, c("BW (MPE=3%, R-squared=0.77)",
"MY (MPE=5%, R-squared=0.80)", pch=c(2,3))
I would liek to express R-squared as R2 as we normally have in the text.
It will work if you combine bquote and as.expression:
plot(1:10)
legend(2, 10, c(as.expression(bquote("BW (MPE = 3%," ~ R^2 ~ "= 0.77)")),
as.expression(bquote("MY (MPE = 5%," ~ R^2 ~ "= 0.80)"))),
pch=c(2,3))
This is less complex than using c( as.expression ( bquote... multiple times:
plot(1:10)
legend(2, 10, expression("BW (MPE = 3%," ~ R^2 ~ "= 0.77)",
"MY (MPE = 5%," ~ R^2 ~ "= 0.80)"),
pch=c(2,3))
It is useful to understand that the expression function is really a way to make lists of expressions and that commas are therefore reserved as separators for that process. This means you cannot have a "naked" comma in something you want to be inside one of the distinct elements. The commas immediately after the %-signs are protected from parsing by the quotes. This could fully plotmath()-ified with:
plot(1:10)
legend(2, 10, expression(BW * list(MPE == 3*'%',
R^2 == 0.77),
MY * list( MPE == 5*'%',
R^2 == 0.80)
),
pch=c(2,3))
That way the only character needing special attention is the '%'-sign because plotmath() uses that character to delimit items in the list of 'special' math tokens. See ?plotmath for the full list.
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.
How to show $\{ X_t \}$ of Latex in the title of a plot of R?
For example
plot(slot(x,"GRID"),slot(x,"PATH"),type="l", xlab="Time t",ylab="X",
main=paste("Simulation of \{X_t\}"))
Thanks!
Assuming you will provide meaningful arguments for the slot expressions, then I think there is a reasonable chance that this is what you want:
plot(1:10,1:10,type="l", xlab="Time t",ylab="X",
main=expression("Simulation of {"*X[t]*"}"))
This is a plotmath expression that presents "t" as a subscript of "X" and that expression is enclosed in curley braces. If I have misread your request, then note that the "*" character is a separator in the plotmath syntax and the braces are simply characters than can be deleted. (The LaTeX expressions don't make a lot of sense to those of us who just use the plotmath syntax, so describing what you want in prose or mathematical jargon would work better for any clarifications.)
To have R emulate LaTeX typesetting, see the examples and methods described in ?plotmath.
To actually have LaTeX typeset your plot text, use the tikz() graphics device provided by the tikzDevice package.
The group() plotmath function can be used to formalise DWin's answer. This solution has the advantage (IMHO) of not using strings as part of the expression. Instead we use the ~ operator to add spacing (whitespace is ignored).
plot(1:10, 1:10, type = "l", xlab = "Time t", ylab = "X",
main=expression(Simulation ~ of ~ group("{", X[t], "}")))
The bgroup() plotmath function provides scalable delimiters but is used in the same manner as the example code above.
Edit (In response to ran2's comment):
The same approaches can be used in ggplot and lattice, using the power of plotmath, just as with base graphics. E.g.
require(ggplot2)
df <- data.frame(A = 1:10, B = 1:10)
qplot(A, B, data = df,
main = expression(Simulation ~ of ~ group("{", X[t], "}")))