Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_point with many data points, the file size of the plot is too big.

Tags:

r

ggplot2

I have a scatter plot with many data points in it. Once I try to zoom in or save it in a pdf format, it takes a long time to open and the pdf is too large. How can I reduce the file size or make the plot lighter by keeping the same visuality of the plot. Here is an example:

library(ggplot2)
df <- data.frame(x = rnorm(50000),y=rnorm(50000))
ggplot(df,aes(x=x,y=y)) + geom_point()
like image 761
Mohammad Avatar asked Sep 06 '25 07:09

Mohammad


1 Answers

Not sure what you mean with visibility. But what about the following?

library(ggplot2)
df <- data.frame(x = rnorm(50000),y=rnorm(50000))
ggplot(df,aes(x=x,y=y)) + geom_point()
ggsave('C:/yourpath/test.png', dpi = 600)

This should result in a .png file of roughly 85 kb.

If you want to increase the quality of the picture, you can increase dpi into 1200 or higher.

like image 147
wake_wake Avatar answered Sep 07 '25 20:09

wake_wake