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.
Related
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.
I have a plain R script, not written using R Markdown. In it I create several graphs using ggplot. I then knit the script via File > Knit Document.
I have two problems with the output. First, the plots are not as wide as the rest of the output. Second, the resolution of the plots is not very good.
Is there any way I can increase the width and resolutions of the graphs without having to rewrite the script using R Markdown?
You can use the knitr spin syntax to provide chunk options for your plots. This should increase their width (and probably also fix the resolution issue):
#+ fig.width = 3
plot(1) # narrow plot
#+ fig.width = 9
plot(1) # wide plot
I have over 2 milion rows dataset and 15 variables. After I create plotly boxplot and histogram for all of 15 variables and generate html out of R Markdown I get 1GB file which is useless. Browser is not able to open it.
Is there any row/variable limit to work with R Markdown?
Is there any way to optimize plotly graphs?
On the 10.000 sample works fine.
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.
How can I force wireframe panels to produce single plots instead of one panel plot/grid plot? The reason is that if I have to produce a Sweave/ Pdf File the original wireframe plot, which R produces and which you can see in my other post
Faceted Lattice Plots in R, e.g., wireframes: How to remove strips and add 1-Line subtitles.
will look very small, especially if I have many many single wireframe plots. I can handle single plots more easily in Sweave.
Lattice allows you to specify the number of columns and rows for the plots which then spill over onto adjacent pages if a multi-page device is used:
pdf("nine.pdf", onefile=TRUE, paper="special")
wireframe(pred~Sepal.Width+Petal.Width|interaction(Species,Petal.Length),
pd, drape=FALSE,scale=list(arrows=FALSE), subset=(Species=="setosa"),
layout=c(1,1,9))
dev.off()
On the console device they create new plots which stack up in the plot device and you can "scroll-back" with keystrokes that may vary depending on your unstated OS. The eps format is accessible using directions in ?ps.