Compress the file size of pdf plot in R [duplicate] - r

This question already has answers here:
Reduce PDF file size of plots by filtering hidden objects
(3 answers)
Closed 6 years ago.
I have saved several pdf plots with large file size in R, the problem is that I will need to import them into Latex, which takes a lot of time. I am wondering how to save a plot with smaller file size in R? Thanks.
Example 1.
seasonplot(ts(hdemand$Demand,frequency=24),
col=rainbow(length(hdemand$Demand)/48))
dev.print(device=pdf,file="hourdemand.pdf")
dev.off()
Example 2. (Even the fitted plot takes times because of the size of data)
par(mfrow=c(1,2))
plot(data$Temp,all.data$Demand)
abline(lm(data$Demand~data$Temp), col="red")
plot(data$APX,data$Demand)
abline(lm(data$Demand~data$APX), col="red")
dev.print("LR.pdf",device=pdf)
dev.off()

Reduce the number of points you are plotting.
But if you can't do that, and don't care about how it would look when zoomed in, write out as png at a convenient dpi and then convert to pdf outside R.

Related

How to save ggplots in PDF with less size R?

I am having trouble saving numerous ggplots inside pdf because I am creating ggplots (scatter plots and boxplots) with 12 million rows (lots of observations).
The problem is when I save the plot as PDF using:
ggsave("my_plots.pdf", myArrangedPlots)
The pdf size is very large = 90 MB for only 120 pages of PDF
When I save one plot as PNG using:
ggsave("plot1.png" plot1)
The size is much less in comparison to saving same single plot in PDF (1MB vs 0.1 MBs)
I think the reason is that ggplot internal mechanism tries to save the plots in Vectorized format format inside the PDF file to get maximum resolution but I don't need that much of resolution. Also note that when there are million of points represented in Vectorized format the size is going to be greater than the same plot in PNG, because PNG doesn't save layers.
I want to save the plots in PDF format but embedding the plots as PNGs instead of Vectorized format to make the PDF file size smaller.
I there any parameter in ggplot2 to achieve this or is there any workaround?
Observing the documentation of pdf(), it's parameters seem to be compatible with ggsave().
I found a parameter which is useDingbats, by default it is set to FALSE but If you set it to TRUE, the PDF size reduces drastically from 94 MB to 10 MB in my case.
So I use it like this:
ggsave("myplots.pdf", arrangedPlots, useDingbats = TRUE)
NOTE: setting useDingbats to true what does is using Dinbats font for small circles, which in case of the scatter plots and boxplots with lots of outlier points reduces the size of final PDF a lot.

How to download clear plot through rstudio?

I plot in rstudio, cannot see the number in plot, then I download as bigger size.
Whatever the png or jpeg format, even size is 2048 pixels, still cannot read the number in plot.
How to solve this problem?
RStudio has known sizing issue of saving pictures plotted in the "plots" window. What I would do is to save the picture directly in code.
Refer to this SO question
In short, what you can do is:
png(filename = pathToYourOutputPNG) # create a png device
plot() # write your plotting functions here
dev.off() # close the device

Elegant way to save mosaic plots? [duplicate]

This question already has answers here:
How to save a plot as image on the disk?
(11 answers)
Closed 5 years ago.
Most of my plots are made with ggplot2 and the ggsave command saves them where they should be with one line. However, mosaic plots using the vcd package are best for my data.
Problem: I don't get an error message with the following code. R says that it has saved my plot, but the plot that gets saved is the last ggplot plot I created, not the mosaic plot I want. Of course I can manually save in RStudio, but I'm quite sure there is a better way. Any ideas?
Onlyaround <- subset(prepData, preposition=="around")
attach(Onlyaround)
mytable <- table(exp_group, session, result)
ftable(mytable)
mosaic(mytable, shade=TRUE, legend=TRUE, main = "Around by Group")
margin.table(mytable)
ggsave("pics/around_mosaic.png")
detach(Onlyaround)
ggsave() is in fact the command to save ggplots, so it's no surprise that it doesn't save you mosaic plot. The standard 'R' way to save plots will work just fine:
jpeg("pics/around_mosaic.png")
mosaic(mytable, shade=TRUE, legend=TRUE, main = "Around by Group")
dev.off()

How to influence the aspect ratio of R's plot() output with R itself or tikzDevice? [duplicate]

This question already has answers here:
How to define fixed aspect-ratio for (base R) scatter-plot
(2 answers)
Closed 1 year ago.
I am not sure if this question is related to R or tikzDevice.
I simply use R and the command plot(1).
The resulting graphic behave like a vector graphic. It's aspect ratio depends on the ratio of the window it is displayed in.
Can I set the aspect ratio explicite while calling plot()?
xlim doesn't affect the graphic output itself - just the content (the numbers on the x-axis).
Maybe there is a way with tikzDevice to fix the aspect ratio?
The script I use with tikzDevice looks like this.
#!/usr/bin/R -f
# load the library
library(tikzDevice)
# the output file
tikz('plot.tex')
#
plot(1, xlab='X-Axis', ylab='Y-Axis')
dev.off()
#Moderators: Please add the tag 'tikzDevice'.
The help page ?plot includes the asp argument which is used to control aspect ratio (more details are on the ?plot.window page).
I don't know if the tikz device handles this differently (because the height and width of the plotting are may change after the plot is created), but the place to start is trying something like:
plot(1, asp=1)
and then make sure that the size of the plotting area is the same in the final document.
Edit
You may not see much change in the plot when you only plot 1 point, but try the following commands to see the effect of setting asp:
theta <- seq(0,2*pi, length=200)
plot(cos(theta),sin(theta))
plot(cos(theta),sin(theta), asp=1)
For regular R and Rstudio at the defaults (on my computer at least) there is a visible difference in the 2 plots. I have not tried this with a tikz device.
You might also try the squishplot function from the TeachingDemos package (I have never tried it with tikz devices, but in regular R it works as another way to set the aspect ratio):
library(TeachingDemos)
op <- squishplot(c(-1,1),c(-1,1), asp=1)
plot(cos(theta),sin(theta))
par(op)

Plot in Sweave PDF document is lagging my computer [duplicate]

This question already has answers here:
Making flattened pdfs in Sweave
(3 answers)
Closed 9 years ago.
I am trying to include a single, less than one-page-sized plot into a Sweave/R pdf document. This plot is based on huge amounts of data - i.e. in a small plot area there are tens of thousands of points. Whenever I include the plot normally through Sweave, I get huge lag when I open the resulting pdf. This is similar to the case of exporting an eps with tens of thousands of dots - even though the plot area is small it will lag heavily.
How do I code it such that a png is inserted, or equivalent, which doesn't keep all the information of every dot in the plot but just keeps the information of the pixels corresponding to the plot size?
\begin{figure}
\begin{center}
<<fig=TRUE,echo=FALSE,height=4>>=
plot(rnorm(100000))
#
\end{center}
\caption{Visualisation in Sweave which can lag computers}
\end{figure}
I am looking for a LaTeX solution. This means no PNG
Use png like:
\begin{figure}
\begin{center}
<<label, fig=FALSE>>=
png('label.png')
plot(rnorm(100000))
dev.off()
#
\end{center}
\includegraphics{label}
\caption{Visualisation in Sweave which can lag computers}
\end{figure}
Or use the Sweave driver from here.
An alternative (not a direct answer to the question asked) is to replace a scatterplot with large numbers of points with a hexagonal binning plot instead. The hexbin package (bioconductor) or the ggplot2 package both have functions for creating the hexagonal binning plots. These plots will be smaller/faster than a scatterplot that contains many points, and for that many points the hexbin plot may even be more meaningful.

Resources