R export graph to pdf with page labels and import into LaTeX using page labels - r

I was wondering if it is possible to set page labels in PDFs exported from R and import the PDF page as an image using said labels into LaTeX?
What I want to do is:
export several graphs from R to a single multi-page PDF file
label the pages in the PDF
Idea:
PDF("multipage.pdf")
graph_object_1 (label="bubblechart")
graph_object_2 (label="bargraph")
graph_object_3 (label="scatterplot")
dev.off()
import as graph into LaTeX using the label instead of a page number
\includegraphics[page={bargraph}]{multipage}
I am aware that I can access pages using the respective page number. But it is common that the graphs change over time, more are added, some are removed. And adapting the page numbers in the TeX file every time would be painful.
I know about Sweave and knitR but my co-authors don't use R and they are therefore not an option.

I am wondering why you want to use a single pdf file. Why not
pdf("bubblechart.pdf")
...
dev.off()
pdf("bargraph.pdf")
...
dev.off()
and then
\begin{figure}
\includegraphics{bubblechart}
\end{figure}
and so forth...
LaTeX would do all the numbering and you have maximal flexibility...

Related

How to capture a cell's output for use in another cell?

In org-mode, I can name the output of a code block and include it elsewhere in the document.
Is it possible to do this (or something similar) in a colab .ipynb file, or within a Jupyter notebook in general?
For example, I can make a cell with some Python code that generates a plot:
import matplotlib.pyplot as plt
plt.plot([0,2,1,4,9])
After executing, the plot appears below the code cell. So far so good.
But how do I capture this plot and to use it elsewhere in the notebook?
My hope is there is some syntax so that I can include the plot in a markdown cell, for example:
# this is my title
As you can see, the numbers go up and down in the plot:
![][some_magic_link_here]
Here is some more text to explain the plot.
Does such a feature exist in colab?
Good news - embedding an image in another markdown cell is self-service. Here's an example:
Full notebook:
https://colab.research.google.com/drive/1PF-hT-m8eZN2CBzFkMp9Bvzj9pSBQVYn
The key bits are:
Using IPython.display.Markdown is order to programmatically construct the markdown.
In addition to rendering, save the matplotlib figure using savefig.
Include the image data in the markdown after base64 encoding.

How to plot many plots to a pdf and AND a subset to Rmarkdown report?

I need to generate about >50 plots. I want to save them all in one pdf and have just a few of them appear in my R markdown html output.
I can print many plots to a single pdf like this:
pdf("path/to/out.pdf")
for (sam in samples){
plot(sam) # simplified stand-in for many lines of code
}
dev.off()
That puts one sample's plot on each page of a pdf that I can flip through later.
I want some of those plots to appear in my markdown, without duplicating the plot code.
I've tried:
pdf("path/to/out.pdf")
count=0
for (sam in samples){
plot(sam) # simplified stand-in for many lines of code
if (sample(1:10,1)==1){ #randomly select a few examples
count = count + 1
dev.copy(which=dev.prev()) # to revert back to the default device
dev.set(dev.next()) # to resume printing to the same pdf
}
}
dev.off()
With this, the pdf is correct and count is >0, but the plots do not appear in the markdown report.
I think something from the dev.copy() family will do what I want but I can't seem to make it work. In other variants I tried, dev.print() complains about only printing from a screen device, or I get errors saying that I cannot copy to the same device, or cannot copy to a null device.
Similar questions (such as Saving plot as pdf and simultaneously display it in the window (x11)) often want to do the reverse: print to screen a then to a pdf. And they generally only deal with one plot.
I want to make several plots, and I want ALL of my plots to go to the pdf, but only a FEW to go to the Rmarkdown html report as an example of what is in the pdf.
I'm working on a mac using R 3.5.1, RStudio v1.1.456
Thanks in advance!

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

Export plots to pdf with bookmark for each plot

I'm trying to export multiple plots (about 30 or so) into a single pdf file - the catch is that I would really like bookmarks for each plot, so that it's easier to navigate in the pdf. The plots are created with ggplot2 .
I know I can export all plots at once with pdf("myfile.pdf") and dev.off() , or each plot to its own file with ggsave but adding bookmarks in the pdf file is beyond me.
Any ideas?

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.

Resources