Compression options in ggplot2

1,400 views
Skip to first unread message

wade ross

unread,
Apr 3, 2015, 6:36:58 PM4/3/15
to ggp...@googlegroups.com
Is it possible to use a compression argument to export files with the ggsave function?

For example, I can use the following code and compress the file using the lzw compression argument. 

tiff(filename = "yourfilepath", width = 8.5, height = 5, units = "in", pointsize = 12, res=1000, compression="lzw")
plot
(a plot)
dev
.off()

Brandon Hurr

unread,
Apr 3, 2015, 8:30:11 PM4/3/15
to wade ross, ggplot2
Did you try adding that argument to the ggsave() and see if it works? 

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

wade ross

unread,
Apr 6, 2015, 9:25:32 AM4/6/15
to ggp...@googlegroups.com, cwad...@gmail.com
Yes. I did not get an error message, but the file size was not reduced either. 

Brandon Hurr

unread,
Apr 6, 2015, 10:53:02 AM4/6/15
to wade ross, ggplot2

On Mon, Apr 6, 2015 at 6:25 AM, wade ross <cwad...@gmail.com> wrote:
width = 8.5, height = 5, units = "in", pointsize = 12, res=1000, compression="lzw"

Sorry Wade, last week was busy so I could only guess. After further google-fu you aren't the first to try this and have difficulty. 

I did find this: 

I'm on a mac so this makes sense for me: 
#creation of dataset and plot
df<-data.frame(x=1:10, y=1:10)
ggplot(data=df, aes(x=x, y=y))+geom_point()
#170 MB file
ggsave("compressed.not.tiff", plot = last_plot(), width = 8.5, height = 5, units = "in", pointsize = 12, dpi=1000)
# 443 kB file
ggsave("compressed.is.tiff", plot = last_plot(), width = 8.5, height = 5, units = "in", pointsize = 12, dpi=1000, compression="lzw", type="cairo")

I don't have a windows or Linux machine so I'm unsure how useful this will be for you.

B

wade ross

unread,
Apr 6, 2015, 11:09:13 AM4/6/15
to ggp...@googlegroups.com, cwad...@gmail.com
Thanks for searching around, Brandon. I guess I will try and compress the files on my PC at work and see if that works. 
I just wanted to reduce file size for uploading figures to a journal. They are pretty large as is. Cheers. 

Brandon Hurr

unread,
Apr 6, 2015, 11:12:39 AM4/6/15
to wade ross, ggplot2
If that is the case I remember seeing someone on StackOverflow suggesting saving as a PDF (vector based, so the files would be very small too). Try that as well and compare the results. 

Also, there is this which suggests eps as well. 

--

wade ross

unread,
Apr 6, 2015, 2:31:51 PM4/6/15
to ggp...@googlegroups.com
The journal recommends .tiff for reasons unknown. 
For now, I will use the following code which SUBSTANTIALLY reduces file size: (note: it does not use the ggsave function)
tiff(filename = "yourfilepathandfilename.tiff", width = 8.5, height = 5, units = "in", pointsize = 12, res=1000, compression="lzw")
ggplot
(...) # This is the ggplot figure code
dev
.off()  # This closes the tiff device after drawing the figure
Reply all
Reply to author
Forward
0 new messages