减少ggplot中图的大小

时间:2018-09-27 07:17:14

标签: r ggplot2 plot

我想减小我创建的GGplot的大小。大小超过50mbs,我认为大小有问题,请帮助

data=read.csv("C:/Users/Muhammad Faisal/Desktop/WP07/FORMC.csv",header=T)
tiff('FORMCOBSPRED.JPEG', units="in", width=4, height=3, res=1200)
ggplot(data=data, aes(y=Enalapril, x=Predicted))+theme_bw()+ geom_abline( size=1,col="black",linetype=(1))+
  geom_point(data=data, colour="black", size=1)+
  theme(axis.title.x = element_text( size=12),
        axis.text.x  = element_text( vjust=0.5, size=12,colour = "black"))+
  theme(axis.title.y = element_text( size=12),
        axis.text.y  = element_text( vjust=0.5, size=12,colour = "black"))+
  scale_shape(solid =F)+geom_smooth(method="lm",se=F, size=1, colour="red",linetype=(2))+
  ylab("Observed enalapril (ug/L)")+xlab("Predicted enalapril (ug/L)")+ scale_y_continuous(breaks=seq(0,120,30))+ scale_x_continuous(breaks=seq(0,120,30))+
  theme(plot.title = element_text(hjust = 0.5 ))
dev.off()

1 个答案:

答案 0 :(得分:1)

tiff不在乎您如何命名文件,它会另存为tiff,从而导致文件过大。因此,您应该将tiff('FORMCOBSPRED.JPEG', units="in", width=4, height=3, res=1200)更改为jpeg('FORMCOBSPRED.JPEG', units="in", width=4, height=3, res=1200)。如果使用ggsave,它将根据您的文件扩展名将其保存为jpeg。参见下面的比较:

library(ggplot2)
# tiff will result in huge size, even though you name it .jpeg:
tiff("iris_tiff.jpeg", units="in", width=4, height=3, res=1200)
qplot(x = Sepal.Width, y = Sepal.Length, color = Species, data = iris, geom = "point")
dev.off()
file.size("iris_tiff.jpeg") # size is 51840192

# using jpeg() solves that Problem:
jpeg("iris_jpeg.jpeg", units="in", width=4, height=3, res=1200)
qplot(x = Sepal.Width, y = Sepal.Length, color = Species, data = iris, geom = "point")
dev.off()

file.size("iris_jpeg.jpeg") # size is 563329

# if we use the functionality of ggsave, which automatically determines type given extension:
ggsave(filename = "iris.jpeg", 
       plot = qplot(x = Sepal.Width, y = Sepal.Length, color = Species, data = iris, geom = "point"),
       units="in", width=4, height=3, dpi=1200)

file.size("iris.jpeg") # size is 563329, hence same as jpeg()

# same for tiff-extension:
ggsave(filename = "iris.tiff", 
       plot = qplot(x = Sepal.Width, y = Sepal.Length, color = Species, data = iris, geom = "point"),
       units="in", width=4, height=3, dpi=1200)

file.size("iris.tiff") # size is 51840192, hence same as tiff()