Problems with text in R plots and LaTeX - r

Hello stackoverflow community,
I have trouble with the interaction between text in R plots and the Latex Environment. In R, I programmed a simple distributional plot and added the percentage below a given threshold via the command mtext(expression(paste("x% < 0 < 1-x%")), ...) . Then the graph has been saved as eps.-figure in order to Transfer it to Latex. The latter, however, displays the < and > symbols as inverted exclamation marks (usually, the < are obtained by the command \textless outside the mathmode in Latex).
I further added some font packages in Latex and it becomes possible to type the < signs into the text without drawing on the math mode. In the Graphs from R, however, the inverted exclamation marks remain.
Does anybody knows how to solve this issue? Although I think that it is a general problem between the R text and Latex, please let me know if some code helps to understand the Problem.
Thank you very much for your help and suggestions.

I don't think it is a general issue in the communication between R and LaTeX, although I agree it can be tricky. I wasn't able to reproduce the behavior you described using what I normally do to create graphics for TeX-documents.
Maybe this setup can also solve the problem you describe. Below is an example to print the said expression using Cairo and PDF graphics.
R-script:
library(Cairo)
Cairo(file="pic", type="pdf", dpi=100)
x <- y <- 1:5
plot(x,y)
mtext(expression(paste("x% < 0 < 1-x%")))
dev.off()
TeX-file:
\documentclass[12pt,paper=a4]{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}[tbp]
\includegraphics{pic.pdf}
\end{figure}
\end{document}
The resulting PDF that is compiled using pdflatex appears to show the expression correctly. A quick screenshot:

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 render qcc plots to HTML using RMarkdown and knitr

First SO question so please go easy.
I am running the qcc package with Rstudio and I would like to create a report on the graphs that I am generating. I have no problem generating the graphs and exporting each individual graph to pdf for example. I also have no problem creating a HTML document with RMarkdown and Knitr with other plots.
However, with qcc package, I cannot do this! Hopefully someone out there has had this issue and solved.
Here is example from ?qcc that can be used in an RMarkdown file with the issue I am seeing:
data(pistonrings)
attach(pistonrings)
diameter <- qcc.groups(diameter, sample)
qcc(diameter[1:25,], type="xbar")
qcc(diameter[1:25,], type="xbar", newdata=diameter[26:40,])
q <- qcc(diameter[1:25,], type="xbar", newdata=diameter[26:40,], plot=FALSE)
plot(q, chart.all=FALSE)
Thanks in advance for support.
So, I found the reason for this eventually. When function dev.off() is called in the Rmarkdown file then the base plot function will not work!...I didn't include the dev.off() in my question as it was at the top of my script and I hadn't spotted it.
Sorry for the confusion and I hope this helps somebody else.

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.

Mathematical expression in axis label

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