Mathematical expression in axis label - r

In R, I use expression(theta[l]) so that the label of my plot axis is that same as $\theta_l$ from LaTeX. For esthetic reasons, I'd rather like to display $\theta_\ell$. Can you help me?
EDIT
Before, I did
plot(1:10, 1:10, xlab=expression(theta[l]))
and I exported the resulting picture in pdf. Then, using
\begin{figure}[htbp]
\centerline{\includegraphics[scale=.6]{test.pdf}}
\end{figure}
my picture was inserted in LaTeX.
Following the comments, here is what I now do:
require(tikzDevice)
tikz("test.tex", standAlone=TRUE, width=5, height=5)
plot(1:10, 1:10, xlab="$\\theta_\\ell$")
dev.off()
tools::texi2pdf('test.tex')
system(paste(getOption('pdfviewer'),'test.pdf'))
However, when I insert the resuling plot in LaTeX, the quality of the figure is not as good as before. Is there something more that I can do?

There is really no reason to use a possibly unmaintained package like 'tikzDevice' for such a simple problem. Part of the problem with the 'tikz' device is that it doesn't seem to correctly accept the 'xpinch' and 'ypinch' arguments that specify your plot's resolution.
There is a larger question of adding LaTEX notation to plots, but for this localized problem, the question is one of specifying the font to make the base 'plotmath' package display cursive letters for you.
You can change the font for your x-axis label by separating it out from the plot command and choosing a custom font from within the 'title' function with something like this:
plot(1:10, 1:10, xlab="")
windowsFonts(script=windowsFont("Script MT Bold"))
title(xlab=expression(theta[l]), family="script")
What we've done is to specify a null label for the x-axis in the plot command to first make space. Then, we load up a system font into the available font families (I like Script MT Bold for expressions). Finally, we can use the 'title' function to plot the x-axis label and specify the family for any text in that label.
By doing this, we preserve all of the original functionality of the plotting device, so you should no longer have a drop in resolution when converting to PDF.
Now if anyone has a good solution to the LaTEX notation problem outside of the 'tikzDevice' package, I would love to hear about it. The only way I know to do this well is to flip the model and use the 'tikz' LaTEX package to draw the whole graphic manually from within the LaTEX file or to use the 'pixmap' R package to draw an image of my expression on top of the plot. Neither feels like a perfect approach.

I think tikzDevice is the way to go here. You can install from R-forge.
install.packages("tikzDevice", repos="http://R-Forge.R-project.org")
The tikz /pgf philosophy is to create a plot that can be typeset. You will probably want these to be consistent with your document. Eg, with the same packages, fonts, font size etc
You can set these things within a call to tikz by setting
options such as the document declaration
options(tikzDocumentDeclaration = "\\documentclass[10pt]{article}")
packages
tikzLatexPackages (or similar)
You can also control the font size.
All these things are detailed in
?tikzDevice.
You could also use knitr to create your plots within a literate programming document (.rnw)
In this case, using a tikz device a pdf is created (as external = TRUE), using the same document declaration and header / packages as the whole document.
\documentclass[12pt,a4paper]{article}
\begin{document}
<<setup, include = FALSE>>=
opts_chunk$set(dev = 'tikz', external = TRUE)
#
<<myplot, fig.width=5, fig.height = 5, echo=FALSE>>=
plot(1:10, 1:10, xlab="$\\theta_\\ell$")
#
\end{document}

This is a somewhat dirty solution, but it makes it:
plot(1,1, xlab=expression(theta))
title(xlab=" \u2113",line=3.2,cex.lab=.7)
First plot with the theta symbol. Then add the \ell symbol with smaller font size and manually setting the position.

I found a workaround here. They do explain a lengthy process to get the encoding to work with the standard pdf device. Otherwise, the CairoPDF device can be used by installing the Cairo package. Then something like xlab="\u2113" will show up in the pdf using #Julián Urbano's solution. I had no luck using the character within an expression.

Related

Julia Plots.jl Latex issue

Plotting in Julia has been a pain for me thus far. I do
using Plots.jl
Initially the plotting was fine, even with latex expressions. However, something must have changed (I don't know what), and now when I wish to plot something with latex expression in the axis or title, for example:
plot(
p1,p2,
layout=layout,
link=:y,#aligns y-axis gridlines
size = (800, 400),
xtickfontsize=15,ytickfontsize=15,
xlab="s " *L" [h^{-1}Mpc]",
ylab=L"|x_s^2\xi_l^{bisector ,(2)}(s)/\xi_l^{pp}(s)|")
I get the error message:
latex: major issue: So far, no MiKTeX administrator has checked for updates.
latex: failed to create a dvi file
Furthermore, my plots are not longer being shown in Jupyter notebook (although I save them, and when I look at what was saved, the plots are there, although with messed up Latex).
Is there any easy fix for this?
I have tried using PyPlots (and PyCall to call it from python) but seem to have an issue importing that. For now I would prefer to stick with pure Julia plotting libraries, and be able to use them with latex.

How to literally print superscripts in R not used in labels or legends?

I know that in a graph one can use expression() to include superscripts, e.g.
plot(rnorm(20), xlab = expression(paste("n"^"th")))
However, I literally want to print out superscripts, so I can store them in rtf files later. In fact, I want to use them to indicate the degree of statistical significance associated with my regression coefficients (you know, like "0.24^*").
With "expression(paste("n"^"th"))", I tried print(), cat(), eval(), and get() but none of them worked.
I am currently using the "rtf" package to output ".doc" files. If someone suggests using other ways/packages, it is OK if R itself cannot display rich formats in the console, but the results can be stored in some files anyway.
As you've seen, the ?plotmath expressions only work within plots. Nothing in R is optimized for rtf output.
But reading the RTF vignette, it's clear you can write rtf commands in your output and there is a command for superscripting. Try
library(rtf)
rtf<-RTF("text.doc")
addText(rtf,"Hello\n")
addText(rtf,"0.05{\\super*}\n")
addText(rtf,"0.15*\n")
addText(rtf,"End")
done(rtf)
plot(rnorm(20), xlab = as.character("n^*"))
or even
plot(rnorm(20), xlab = paste0("n","^","th"))
Not sure why you want to use expression()

Howto set paper size using postscript() to make it same with pdf() in R

I have already successfully plot a figure using this command:
pdf("myplot.pdf",paper="a4",height=20.0)
The figures are well placed with the proper size orientation
and size. But when I convert it to postcript with the same
parameter, it goes wrong.
postscript("myplot.eps",paper="a4",height=20.0)
What's the correct way to set the parameter in postscript()
so that it generate the same format as pdf()?
I recommend you really to use knitr. One of the motivation of the package is :
I wished for many times that if only I could use graphics devices
other than PDF and postscript; now the dream has come true in the
official R, but what I was hoping for was an option as simple as dev =
'png' or dev = 'CairoJPEG'.
For example you can create a chunck
<<dev='postscript'>>=
plot(cars) ##replace this by your code to create myplot
#

Writing a special expression in R including using LaTeX calligraphic fonts [duplicate]

In R, I use expression(theta[l]) so that the label of my plot axis is that same as $\theta_l$ from LaTeX. For esthetic reasons, I'd rather like to display $\theta_\ell$. Can you help me?
EDIT
Before, I did
plot(1:10, 1:10, xlab=expression(theta[l]))
and I exported the resulting picture in pdf. Then, using
\begin{figure}[htbp]
\centerline{\includegraphics[scale=.6]{test.pdf}}
\end{figure}
my picture was inserted in LaTeX.
Following the comments, here is what I now do:
require(tikzDevice)
tikz("test.tex", standAlone=TRUE, width=5, height=5)
plot(1:10, 1:10, xlab="$\\theta_\\ell$")
dev.off()
tools::texi2pdf('test.tex')
system(paste(getOption('pdfviewer'),'test.pdf'))
However, when I insert the resuling plot in LaTeX, the quality of the figure is not as good as before. Is there something more that I can do?
There is really no reason to use a possibly unmaintained package like 'tikzDevice' for such a simple problem. Part of the problem with the 'tikz' device is that it doesn't seem to correctly accept the 'xpinch' and 'ypinch' arguments that specify your plot's resolution.
There is a larger question of adding LaTEX notation to plots, but for this localized problem, the question is one of specifying the font to make the base 'plotmath' package display cursive letters for you.
You can change the font for your x-axis label by separating it out from the plot command and choosing a custom font from within the 'title' function with something like this:
plot(1:10, 1:10, xlab="")
windowsFonts(script=windowsFont("Script MT Bold"))
title(xlab=expression(theta[l]), family="script")
What we've done is to specify a null label for the x-axis in the plot command to first make space. Then, we load up a system font into the available font families (I like Script MT Bold for expressions). Finally, we can use the 'title' function to plot the x-axis label and specify the family for any text in that label.
By doing this, we preserve all of the original functionality of the plotting device, so you should no longer have a drop in resolution when converting to PDF.
Now if anyone has a good solution to the LaTEX notation problem outside of the 'tikzDevice' package, I would love to hear about it. The only way I know to do this well is to flip the model and use the 'tikz' LaTEX package to draw the whole graphic manually from within the LaTEX file or to use the 'pixmap' R package to draw an image of my expression on top of the plot. Neither feels like a perfect approach.
I think tikzDevice is the way to go here. You can install from R-forge.
install.packages("tikzDevice", repos="http://R-Forge.R-project.org")
The tikz /pgf philosophy is to create a plot that can be typeset. You will probably want these to be consistent with your document. Eg, with the same packages, fonts, font size etc
You can set these things within a call to tikz by setting
options such as the document declaration
options(tikzDocumentDeclaration = "\\documentclass[10pt]{article}")
packages
tikzLatexPackages (or similar)
You can also control the font size.
All these things are detailed in
?tikzDevice.
You could also use knitr to create your plots within a literate programming document (.rnw)
In this case, using a tikz device a pdf is created (as external = TRUE), using the same document declaration and header / packages as the whole document.
\documentclass[12pt,a4paper]{article}
\begin{document}
<<setup, include = FALSE>>=
opts_chunk$set(dev = 'tikz', external = TRUE)
#
<<myplot, fig.width=5, fig.height = 5, echo=FALSE>>=
plot(1:10, 1:10, xlab="$\\theta_\\ell$")
#
\end{document}
This is a somewhat dirty solution, but it makes it:
plot(1,1, xlab=expression(theta))
title(xlab=" \u2113",line=3.2,cex.lab=.7)
First plot with the theta symbol. Then add the \ell symbol with smaller font size and manually setting the position.
I found a workaround here. They do explain a lengthy process to get the encoding to work with the standard pdf device. Otherwise, the CairoPDF device can be used by installing the Cairo package. Then something like xlab="\u2113" will show up in the pdf using #Julián Urbano's solution. I had no luck using the character within an expression.

sweave and ggplot2: no pdfs generated at all

I am trying create a sweave report that contains some graphics done with ggplot2. Though I am looking for some environment for the long run – I just use a simple .Rnw file here that only contains the code and the plot
\documentclass[a4paper]{article}
\SweaveOpts{echo=FALSE}
\usepackage{a4wide}
\begin{document}
\begin{figure}[htbp]
\begin{center}
<<>>=
library(ggplot2)
x=rnorm(100)
qplot(x)
#
\caption{My Graph}
\end{center}
\end{figure}
\end{document}
Unfortunately the graph is not created, I only get a corrupted .pdf and .eps file. Though I get a nice .tex file that appears to work except for the graphics.
I use the following basic code to create it:
Sweave("myfile.Rnw")
I just found some older post on the web that were discussing problems with transparency and sweave / ggplot2 but nothing that could have helped. I also tried the relaxed package, which did not help either. Btw, is there any news on decumar package?
qplot() produces objects, not a graphic output. It might seem like it does when you run it, but that's because without assignment, R is automatically printing the output of qplot(). To integrate it into Sweave, either wrap print() around qplot(), or assign the output of qplot() to something, then wrap that in print().
...
<<fig = T, echo = F>>=
library(ggplot2)
x=rnorm(100)
p <- qplot(x)
print(p)
#
...
That should work. I use ggplot2 graphics in my sweave docs all the time.
You have to wrap it around print() to make it work in sweave.
Actually, while both previous answers are correct, your problem is something else.
You need to ensure that the entire code block is at the left of the page (apart from iundentation in functions). Again, I have no idea why but this causes problems for Sweave.
After ensuring that all code (and header/footer for code chunk) were at the left of the page (and adding a print statement) then your example works for me.
Incidentally, i learned today that you can create an environment around your code in sweave documents (which i wasn't aware of, and will save me much time). Good old stackoverflow, teaching you something new even when you answer a question!
Hope this helps.

Resources