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!
Add Titles to a Graph in R Programming – title() Function title() function in R Language is used to add main title and axis title to a graph. This function can also be used to modify the existing titles. Syntax: title(main = NULL, sub = NULL, xlab = NULL, ylab = NULL, …)
Use the title() function title() can be also used to add titles to a graph. A simplified format is : title(main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ...)
Use the title( ) function to add labels to a plot. Many other graphical parameters (such as text size, font, rotation, and color) can also be specified in the title( ) function. # labels 25% smaller than the default and green.
To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.
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], "}")))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With