I have created a plot from a very large vector (magnitude of 10^7). The problem with the usual way of saving the plot as a pdf file is that the pdf file comes out as a very large file of around 10MB. I don't want such a large size for a simple time series plot. How do I save the plot such that the size is small enough to be at most 100kilobytes?
baptiste is on the right track with their suggestion of png for a nice raster type plot. In contrast to Jdbaba's suggestion of copying the open device, I suggest that you make a call to the png()device directly. This will save a lot of time in that you won't have to load the plot in a separate device window first, which can take a long time to load if the data set is large.
Example
#plotting of 1e+06 points
x <- rnorm(1000000)
y <- rnorm(1000000)
png("myplot.png", width=4, height=4, units="in", res=300)
par(mar=c(4,4,1,1))
plot(x,y,col=rgb(0,0,0,0.03), pch=".", cex=2)
dev.off() #only 129kb in size
see ?png for other settings of the png device.
If you want to plot the png file use the following command:
dev.copy(png,"myfile.png",width=8,height=6,units="in",res=100)
dev.off()
you can change res value to higher value if you want to output high quality graphs.
If you want to save the file as pdf use the following command:
pdf("myfile.pdf",width=8,height=6)
dev.off()
Remember to change the width and height values as needed.
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'm saving a plot in R that I made with ComplexHeatmap. However, when I save it as a PDF, I could see that there are horizontal lines in the annotation bar. I don't have this issue when I save it as a png file but the plot is blurry. How can I save my plot so that it is clear and the lines don't appear?
In R, the resolution of a plot saved as a PDF is related to the size of the PDF page. You can increase the size, and therefore the resolution, by adding to your code this:
pdf(width = 32, height = 18 , onefile = T)
Followed by the plotting code, and ending with dev.off().
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
plot pic
As the pic resolution is not high enough, I cannot read every word in my cluster dendogram.
How to fix this problem?
As #DavidZ said, we can change the resolution like below;
You can save it by
png("plot.png", width=20, height=20, units="in", res=500)
plot(...)
dev.off()
plot.png should be saved in your current working directory.
I have a distribution plot that I want like this:
dev.new(width=5,height=2)
plot(w.count.v, main="Distribution Plot of 'she' in Two Kinds",xlab="Novel Time",ylab=m=c(0,1),yaxt="n")
How do I save it as a .png as the size on the device? When I save it, this is the resulting image.