我有65000 160矩陣,即我然後使用圖像(X)中R.圖像繪圖太大
變換成圖像我也使用選項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 ----- #
謝謝!
使用位圖格式繪圖(最好是??png)? (雖然我不確定這是否會對這種情況有所幫助:'?jpeg'實際上可能會更好)您打算如何傳播此圖 - 即您希望讀者看到哪些細節?是否有非常大的特徵? –
這個問題需要如何創建文件的細節 – mdsumner
@BenBolker謝謝。你是對的,我試圖把它保存爲pdf,這可能是大量空間使用的來源。 jpg和png也適合我。我仍然很想知道如何限制pdf格式的大小。 – Dnaiel