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.
Related
I am using the effects package in R to derive (and plot) the effects of a complicated linear model.
When I use allEffects(linearModel) I can see the results on screen and save it to a pdf file as usual. Since the model has many terms, the output is not useful as it stands.
Therefore I use effects(someTerm, linearModel) to focus on the terms of interest and the results on screen are what I need. However, when saving it to a pdf file, the file contains no useful output (though it does take up 3.5Kb of space). There is no error message from R at the console.
To ease understanding, I created a simple data set and a script for generating the effects plots, in the same way that I tried with the "real" model.
factorData.csv
A,B,C,y
a1,b1,c1,3
a1,b2,c1,4
a2,b1,c1,5
a2,b2,c1,6
a1,b1,c1,2
a1,b1,c2,3.5
a1,b2,c2,4
a2,b1,c2,5.1
a2,b2,c2,6.2
plotEffect.r
require(effects)
dataFile <- '/tmp/factorData.csv'
effectABfile <- '/tmp/effect_AB.pdf'
effectABCfile <- '/tmp/effect_ABC.pdf'
allEffectFile <- '/tmp/lm_eff.pdf'
df <- read.csv(file=dataFile,header=TRUE,sep=',')
linearModel <- lm(y~A*B*C,data=df)
lm_eff <- allEffects(linearModel)
pdf(file=effectABfile)
plot(effect('A:B',linearModel))
dev.off()
pdf(file=allEffectFile)
plot(lm_eff)
dev.off()
pdf(file=effectABCfile)
plot(Effect(c('A','B','C'),linearModel))
dev.off()
As you can see, I tried allEffects, effect and Effect; allEffects is the only one that works for me. Please note that the script assumes that the data is placed in /tmp/factorData.csv - you might need to change the path of course. I also randomised the order in which the plots are generated, with no effect.
I had a look elsewhere on stackoverflow and saving plots to pdfs fails was the closest but the advice there (to issue dev.off() after each plot to 'close' the pdf file) is something I do already, as seen in plotEffect.r.
I tried this script on 2 machines, each running Lubuntu 14.04.1 64-bit with R version 3.0.2 and the latest effects package installed within R using install.packages. The results were the same.
I would be very grateful for advice on how to fix the problem of saving (instances of) this plot type to a pdf.
Fix/workaround
As suggested by #Roland in the comments below, if you wish to save grid plots (such as the output from the effects plots in this instance) into pdf files, it is better/more reliable to generate the plots separately/manually (rather than in a script). It does not appear to be (as much of) an issue for base graphics or even ggplot2 graphics, where I for one have never encountered this problem/needed this workaround in the past. Thanks to #Roland for suggesting this fix!
#Roland also added that Sys.sleep() might help in scripts. Although it did not do so in my case, I discovered that it was possible to paste several such plotting commands and R would run them as a batch, saving the plots to pdf correctly. Therefore, I believe it should be possible to recover much of the benefits of running the script by taking these steps:
Use R to create the text representation of the pdf(), plot() and dev.off() triad of commands
The main script outputs this plotting command text (specific to each instance of a plot) to a file
Open the plotting command text in a text editor
Copy the entire contents of the commands file and paste it into the R console
Optionally, you may wish to use the command line in Step 3 and 4 - How can I load a file's contents into the clipboard? has useful advice.
This two stage procedure is a reasonable workaround, but arguably there should be no need for it.
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:
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.
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.
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.