Output graph to a two page PDF - r

I am doing a forest plot and want to save it to a PDF file.
My forest plot is oversize (8in*20in). It can fit in a one page PDF like this:
dev.print(pdf, file="C:\\Work\\plot.pdf", width=8, height=20);
But then it is too long: When I print this PDF on a A4 paper, it has to be shrinked to fit the paper.
So I want to save it to a two-page PDF file (from R). Ps: it is not a question about how to set the printer.
How to do this?

So, you are able to generate an 8in x 20in == 203.2mm x 508mm == 576pt x 1440pt sized PDF showing a plot.
It is not entirely clear to me from your question what exactly you want:
Generate the PDF plot so that it is divided into two different pages from the beginning?
Take the PDF as is and during the print job setup find these settings which would print it onto two different pages by posterizing the original page?
Post-process the PDF that you created to posterize it and create a 2-page output PDF (which you can then print)?
Assuming '1.': generate PDF plot distributed over 2 pages
Sorry, I cannot help here...
Assuming '2.': print setup to print 1 PDF page on 2 sheets of paper
If you print a PDF from Adobe Acrobat or from Adobe Reader, then you'll find a setting in the print dialog named "Poster". Here you can select to print one PDF page across multiple pieces of paper. (It also lets you select if you want some overlap from piece to piece, and if you want to add cut marks and the like to the printouts).
Assuming '3.': post-process 1 PDF page to stretch over 2 A4 pages
MuPDF is a lightweight PDF (and other document formats) viewer, made by the same company that also maintains Ghostscript. MuPDF ships with an additional command line utility, mutool.
Its subcommand poster can divide PDF pages into smaller tiles and 'posterize' them. So this command will achieve what you want:
mutool poster -x 1 -y 2 input.pdf output.pdf
The output.pdf will be divided into 1 part (i.e. not divided) in x-, and into 2 equal parts in y-direction. (You could divide it into any other number of segments if you wanted). So output.pdf will have two pages, each sized 8in x 10in. A4 paper is sized 8.26in x 11.69in when measured in Inches.
When printing these, you'll still need to enable the Print to fit Page Size checkbox in the print dialog if you want to make best use of the A4 page size.
Ghostscript is a command line tool that can (amongst many other functions) be used to process PDF files (PDF in, modified PDF out). It can be (ab)used to cut PDF pages into halfs.
Here are a few previous StackOverflow answers which describe how to do it. You'll need to adapt some parameters to your specific size(s), but the principles should be clear from those examples (even though some of these split pages into left and right halves, not top/bottom as you may require):
Linux-based tool to chop PDFs into multiple pages (SuperUser)
Freeware to split a pdf's pages down the middle? (SuperUser)
Convert PDF 2 sides per page to 1 side per page (SuperUser)
How can I split a PDF's pages down the middle? (SuperUser)
Cropping a PDF using Ghostscript 9.01 (StackOverflow)
PDF - Remove White Margins (StackOverflow)
Split one PDF page into two (StackOverflow)
The method described there is more tedious and not as straight-forward as with the mutool poster method.

Maybe not the answer you are looking for but you could print it in another vectorial format (e.g. svg) and then export it as pdf on two pages with a (vectorial) image editor.
Edit: If ploting in pdf works well despite the big size of the graph there are also tools to split pdf pages. You can find some directions here:
https://superuser.com/questions/437148/how-to-split-a-pdf-onto-multiple-pages-on-command-line
Windows equivalent of pdfposter could be Rasterbator or PosteRazor, for example.

Related

How to insert a local image into an ioslide presentation in R

This may be impossibly simple but not finding anything that lays out in direct enough fashion. So I have a lengthy string of code and upon using ggsave (look below) produces the desired chart in My Documents folder. What would I put after this in order to have the Figure 2.png to show up on the second slide? At the moment, when I knit it says, "## Saving 7.5 x 4.5 in image".
ggsave("Figure 2.png",Figure6UnEmploymentChart)

Automation of images to PDF at District level

I want create a PDF with 5 Images from 5 folder with text. First I want to read the district name from CSV file and check the same file name in each folder. Second if the file name are matching, Make a PDF with the five images and CSV name as title for the PDF page and text which will be common for all the PDF. I want to give particular font and size, border for images, border for text also. I want to repeat for n number of districts. Is it possible with LaTeX or python?? Can anyone help me please, I am new to coding.
Thanks in advance.
In LaTeX including graphics is done with: \includegraphics and it's fairly straightforward. You can find a number of examples on the linked page above that will walk you through setting the pathname to each of your folders as necessary. There's also a good answer here about how to set multiple pathways in the declaration of your document. As a general note, LaTeX will definitely be more flexible with making pdfs than either r or LaTeX because that's what it was built for.

gnuplot - How can I save a graphics file of a plot that is the same as I designed it in xterminal?

I have been making plots for some time now, and they are precisely the way I like them, on screen. The data is coming in from sensors related to solar power collection and storage.
Plotted on screen they look great so I do a screen region capture to save them.
So now I would like to automate the saving process.
Here is what I have done so far:
I set up a cron job so they would be run right at midnight, capturing the whole day and saving it as a .png file
Then it moves the "today.dat" data file to the archive named by date.
This part is all working as designed.
EXCEPT, by using .PNG the images do not look the same.
I really thought png would be the best option, but it turns out that the font used for the X-axis (HH:MM ticks) is too thick and they run together. It looks like a crayon-drawn version of my plot designs.
Can someone please give me some guidance on how to best programatically generate the plots for saving so they look like the way I designed them?
As pointed out in the comments above, the best way is probably to use a different terminal for output to an image file, and simply ignore the fact that the generated images are not identical to what you see on your screen when using the x11 terminal. However, if you really need an exact copy, there are (at least) two options:
You could automate the process of taking a screenshot. You can even do this from within gnuplot, where it might come handy that the GPVAL_TERM_WINDOWID variable contains the X Windows ID for the current plot window. You can use that to make a screenshot of the window after you made the plot:
system(sprintf("xwd -id 0x%x | convert xwd:- screenshot.png", GPVAL_TERM_WINDOWID))
Here I included a call to convert to convert the xwd file format to png.
Another option is to use the xlib terminal, which saves the sequence of commands that the gnuplot_x11 helper application turns into the window you see on the screen. For example,
set term push; set term xlib; set output "file.xlib"; replot; set output; set term pop
will create the file file.xlib that has all the information of the last plot. To later view this plot, use
gnuplot_x11 -noevents -persist < file.xlib
where you might have to specify the path to gnuplot_x11.
Similar as #user8153 suggested for x11, you can use import, which is as convert an imagemagick tool
system("import -window ".GPVAL_TERM_WINDOWID." screenshot.png")
Convenient is also a shortcut to copy the image into clipboard and paste it with Ctrl+v elsewhere:
bind Ctrl-c 'system("import -window ".GPVAL_TERM_WINDOWID." png:- | xclip -sel clip -t image/png")'
See also Show graph on display and save it to file simultaneously in gnuplot.

Can I reduce pdf file size in knitR/ggplot2 when using a large dataset without using external tools?

I have a number of large-ish files which I am reading into R in an rmarkdown document, cleaning up, and plotting with ggplot2.
Most files are about 3Mb in size with around 80,000 lines of data, but some are 12Mb in size, with 318,406 lines of data (Time, Extension, Force).
Time,Extension,Load
(sec),(mm),(N)
"0.00000","0.00000","-4.95665"
"0.00200","0.00000","-4.95677"
"0.00400","0.00000","-4.95691"
"0.10400","-0.00040","-4.95423"
It takes a while to churn through the data and create the pdf file (that's OK), but the PDF file is now nearly 6Mb in size with about 16 graphs in there (in fact 3 graphs which are facet plots using ggplot2).
I understand that the pdf is including a line segment for every datapoint in my dataset, and therefore as I increase the number of graphs the amount of data in the file increases./ However, I don't forsee a requirement to drill down into the pdf document to see that level of detail, and I will have problems emailing it around as it approaches 10Mb).
If I convert pdf to ps using pdf2ps and then go back to pdf with ps2pdf, I get a file about 1/3 of the size of the original pdf, and the quality looks great.
Therefore is there a method from within R/knitR/ggplot2 to reduce the number of points plotted in the pdf images without using an external tool to compress the pdf file ? (or to somehow optimise the pdf generated ?)
Cheers
Pete
You can try changing the graphic device from pdf to png by adding
knitr::opts_chunk$set(dev = 'png')
to your setup chunk.
Or you can add this to your output header
output:
pdf_document:
dev: png
Try different devices (png, jpg). Maybe this will change the size

Slow rendering with image | MigraDoc

I am creating a PDF using MigraDoc and have now ran into a little problem. I am using a A4 size image (2480px x 3508px / 96KB in size) as a background for my PDF using the following code:
Dim frame = Section.Headers.FirstPage.AddTextFrame
frame.AddImage("background.png")
frame.WrapFormat.Style = WrapStyle.Through
frame.RelativeHorizontal = RelativeHorizontal.Page
Using this causes the PDF to render around 10 times longer (say 10 seconds) then without or a smaller sized file (say 1 second). Is there anyway to speed this up?
I have tried to not use a frame thinking this could be the problem displaying the image using:
Dim backing As Image = Section.Headers.FirstPage.AddImage("background.png")
But still the same results, the reason I want time cut down is I create up to 1000 of these and this can take a long time at the current speed.
I cant downsize the image any more but I don't see why it should be a problem with the size. If this is the problem and there is no way around it please do let me know.
Maybe it goes faster when you use a JPEG file (if that is an option).
JPEG files are copied into the PDF as they are. PNGs and other formats have to be converted into "PDF images".
You can use pages from PDF files just like images. This is another option you can try: once create a PDF with your background image, and create all other files with that PDF instead of the PNG (if JPEG is not appropriate for your image).
There are two builds of MigraDoc: one using GDI+, one using WPF. You could try both to see if that makes a difference.
BTW: Images can be positioned like TextFrames, so there is no need to put an Image into a TextFrame.

Resources