Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show $\{ X_t \}$ in the title of a plot of R

Tags:

r

plotmath

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!

like image 674
Tim Avatar asked Nov 22 '10 04:11

Tim


People also ask

How to make a title for a graph in R?

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, …)

How to add a title to a base r plot?

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, ...)

How to add a label to a graph in R?

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.

How do you add axis titles in R?

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”.


3 Answers

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

like image 167
IRTFM Avatar answered Sep 28 '22 04:09

IRTFM


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.

like image 35
Sharpie Avatar answered Sep 28 '22 05:09

Sharpie


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], "}")))
like image 45
Gavin Simpson Avatar answered Sep 28 '22 06:09

Gavin Simpson