Can't get images from R to latex - r

I've generated a number of plots in R, but two specific ones are giving me fits. I have two point processes from R (.ppp objects) that I've generated plots from, but no matter what file type I save them as, Texmaker will not read them in properly. I've tried using .eps files and the epstopdf package, but would only get an empty plot (only the axes and no points). I've tried saving the plot as a .png, .jpeg, and .pdf using the instructions (minus rsweave) here Getting R plots into LaTeX?, but every time I try to compile, I get the error "no output PDF file produced!" because "reading image file failed". I'm at my wit's end here.
I'm using Texmaker along with miktex 2.9, if that helps.
Edit: Don't use dev.new() after using png() when saving images in R (shown in the link). That is what was causing the load error.

Related

R program from Wikipedia doesn't plot

I want to use a R script which is publicly available from Wikipedia: https://commons.wikimedia.org/wiki/File:Correlation_examples2.svg .
This program is supposed to output an image but for some reason when I paste the code to Rstudio it doesn't plot.
What can I do to make it output the image?
Two main reasons:
The script by itself doesn't plot anything. It just sets up the function that will generate the image. So pasting the code by itself won't help and you need to call the 'output' function.
However the function by itself doesn't plot anything either. It saves the plot as Rplots.svg which contains the .svg code to plot the images.
To display the plot in RStudio, use for example the magick package.
output()
magick::image_read_svg('Rplots.svg')
Alternatively you can open the Rplots.svg file with a browser of your choice.

How to export mulitple PDFs from R

I'm fairly new to R and am running into some issues exporting multiple graphs from R into a PDF file. The graphs I've created are:
plot(mirt.nom.cohen.pf.fa2,type="info")
plot(mirt.nom.cohen.pf.fa2,type="infotrace")
plot(mirt.nom.cohen.pf.fa2,type="trace")
What would be the most efficient way to export these to a pdf?
Any guidance would be greatly appreciated.
Thank you!
Darko
Think of it this way - to save an R graph as a pdf file, you need to:
Open the pdf file;
Place the graph in that file;
Close the pdf file.
What this means in your case is that you have to do something like this:
pdf("plot1.pdf")
plot(mirt.nom.cohen.pf.fa2,type="info")
dev.off()
pdf("plot2.pdf")
plot(mirt.nom.cohen.pf.fa2,type="infotrace")
dev.off()
pdf("plot3.pdf")
plot(mirt.nom.cohen.pf.fa2,type="trace")
dev.off()
The 3 pdf files you created will be stored in R's working directory, which you can find with the command:
getwd()
Once you know where R saved the pdf files, you have to locate that directory on your computer and then you can manually select and open the files. Each file will be single-page and contain one graph.
As already suggested here, you can also create a single, multi-page pdf file which will contain all 3 graphs (one graph per page):
pdf("plot.pdf")
plot(mirt.nom.cohen.pf.fa2,type="info")
plot(mirt.nom.cohen.pf.fa2,type="infotrace")
plot(mirt.nom.cohen.pf.fa2,type="trace")
dev.off()
This file will also be stored in R's working directory.
If you look at the help of the pdf function in R, you'll discover that you can use it with various options (e.g., width and height):
help(pdf)
do you want one to a page, or all on the same page?
to simply get them to a pdf file, you open the pdf device, indicate the file name, and then everything until you close it will go there.
pdf("plots.pdf")
your statements
dev.off()

How to hide figures in knitr, but create them as png?

I am currently doing some statistical analysis in R and use knitr to generate results and an overview document.
There are some additional plots, which I want to be done and saved as a .png (with specified file name and location), but not included in the generated .html file (too many of them, and they are not at the end).
Using dev.copy(png, ...) works fine for generating the plots, but the figures appear in the .html. If I specify fig.keep=none the .png files are created, but blank.
Is there some way to do what I want?
This is from knitr website:
fig.show: ('asis'; character) how to show/arrange the plots; four
possible values are
asis: show plots exactly in places where they were
generated (as if the code were run in an R terminal)
hold: hold all
plots and output them in the very end of a code chunk
animate: wrap
all plots into an animation if there are mutiple plots in a chunk
hide: generate plot files but hide them in the output document
fig.show = 'hide' worked for me.

How to save graphs/plots from quatrz generated by bio3d (R package)

I'm new to R and bio3d and have been unable to find any answers to my problem. I'm trying to find a way to save graphs/plots generated by bio3d (a R package). So far I have to manually click save as when the graph appears and I have tried many variations of R language to save the graph which either result in no saved file or a small file that cannot be opened. Can anyone give me some pointers please?
In an R script you may try with the following lines:
pdf('nameoftheplot.pdf', width=..., height=...)
Then you can write the R-code that generates your plot, and at the end you should add this last line:
dev.off()
Select all the lines and run them with cmd+R (Windows) or cmd+enter (OS X). The output pdf file with the plot should be located in your current working directory. Hope this works.
Edit: if you want a .png file as an output you have to replace the first line with:
png('nameoftheplot.pdf', width=..., height=..., res=...)
Edit2: Example:
pdf("firstplot.pdf", width=6, height=3)
qplot(carat, data = diamonds, geom = "density")
dev.off()
If you have already created your plot (i.e. it is displayed in the active quartz or x11 window) you can use dev.copy2pdf() and it relatives, e.g.
plot(c(1:10))
dev.copy2pdf(file="example.pdf")
If you want to do this without plotting to the quartz/x11 window then issue a call to png() or pdf() etc before your plot() call and then follow it with a dev.off() call, e.g.
pdf(file="example2.pdf")
plot(c(1:10))
dev.off()
The 'plot.new figure margins too large' error can occur when your plot window is too small to take all the graphic output you are trying to produce. Often making your window bigger will solve this.

ggplot with ggplot2: pdf very slow to display

I am producing a pdf plot with this kind of command:
ggplot(df, aes(sample = x))+
stat_qq(geom="point",distribution=qexp)+
geom_abline(intercept = 0, slope = 1,linetype='dashed',col='red')
ggsave(file="xxx.pdf")
Than I want to integrate the pdf into a tex file and produce a final pdf document.
But, the ggplot is very slow to display and makes the pdf crash very often.
When I use geom='line' it doesn't happen so I guess it comes from the number of circle points.
Do you have any idea on how to solve this? I really prefer the geom='point' option.
PDFs are vector based - so every single point on your chart has to be loaded individually. This produces a 'load-up' sort of effect on your PDF. My solution would be to save as a high DPI png/gif instead:
ggsave(file="xxx.png", dpi=400) #default is 300 which is probably sufficent
Tex to pdflatex (or AN Other) will find the file 'xxx' if you not forced an extension in your R to Tex conversion as the include statement will usually not mention an extension. You will need to make sure that the pdf is deleted from the your charts folders to ensure it doesn't get picked up in preference to the png.

Resources