R中的图像图太大了

Dna*_*iel 1 plot r image matrix

我有一个65,000乘160矩阵,然后我用R中的图像(X)转换成图像.

我还使用选项useRaster = TRUE,这使得绘图批次更快,文件也更小.

但是,文件大小仍然相当大~60 Mb.无论如何要控制R中图像的文件大小?如果是这样,我会喜欢听到如何以及通过约束文件大小而失去多少分辨率.

该文件创建为pdf文件,代码如下:

# ----- Define a function for plotting a matrix ----- #
myImagePlot <- function(x, filename, ...){
  dev = "pdf"
  #filename = '/home/dnaiel/test.pdf'
  if(dev == "pdf") { pdf(filename, version = "1.4") } else{}
     min <- min(x)
     max <- max(x)
     yLabels <- rownames(x)
     xLabels <- colnames(x)
     title <-c()
  # check for additional function arguments
  if( length(list(...)) ){
    Lst <- list(...)
    if( !is.null(Lst$zlim) ){
       min <- Lst$zlim[1]
       max <- Lst$zlim[2]
    }
    if( !is.null(Lst$yLabels) ){
       yLabels <- c(Lst$yLabels)
    }
    if( !is.null(Lst$xLabels) ){
       xLabels <- c(Lst$xLabels)
    }
    if( !is.null(Lst$title) ){
       title <- Lst$title
    }
  }
# check for null values
if( is.null(xLabels) ){
   xLabels <- c(1:ncol(x))
}
if( is.null(yLabels) ){
   yLabels <- c(1:nrow(x))
}

layout(matrix(data=c(1,2), nrow=1, ncol=2), widths=c(4,1), heights=c(1,1))

 # Red and green range from 0 to 1 while Blue ranges from 1 to 0
 ColorRamp <- rgb( seq(0,1,length=256),  # Red
                   seq(0,1,length=256),  # Green
                   seq(1,0,length=256))  # Blue
 ColorLevels <- seq(min, max, length=length(ColorRamp))

 # Reverse Y axis
 reverse <- nrow(x) : 1
 yLabels <- yLabels[reverse]
 x <- x[reverse,]

 # Data Map
 par(mar = c(3,5,2.5,2))
 image(1:length(xLabels), 1:length(yLabels), t(x), col=ColorRamp, xlab="",
 ylab="", axes=FALSE, zlim=c(min,max), useRaster=TRUE)
 if( !is.null(title) ){
    title(main=title)
 }
# Here we define the axis, left of the plot, clustering trees....
#axis(BELOW<-1, at=1:length(xLabels), labels=xLabels, cex.axis=0.7)
# axis(LEFT <-2, at=1:length(yLabels), labels=yLabels, las= HORIZONTAL<-1,
# cex.axis=0.7)

 # Color Scale (right side of the image plot)
 par(mar = c(3,2.5,2.5,2))
 image(1, ColorLevels,
      matrix(data=ColorLevels, ncol=length(ColorLevels),nrow=1),
      col=ColorRamp,
      xlab="",ylab="",
      xaxt="n", useRaster=TRUE)

 layout(1)
  if( dev == "pdf") {
    dev.off() }
}
# ----- END plot function ----- #
Run Code Online (Sandbox Code Playgroud)

谢谢!

42-*_*42- 5

当我image在使用jpeg该设备的默认大小的调用内创建此类矩阵和绘图时,我得到一个以KB(90KB)为单位的文件.

> bigm <-matrix(sample(1:8, 65000*160, repl=TRUE),  160, 65000)
> jpeg(filename="test.jpg")
> image(bigm)
> dev.off()
quartz 
     2 
Run Code Online (Sandbox Code Playgroud)

这是否适合您的应用程序可能取决于此任务的确切性质和操作系统,这两者都尚未指定.

  • 我有R创建的pdf也是巨大的.`jpeg`或`png`设备似乎在压缩方面更好.pdf的期望是它们在扩展时不会失去分辨率,而其他设备在有大量粒度时更加经济. (2认同)