4

rglデータの因子レベルごとにパッケージを使用して 3D プロットを作成し、それらを png として保存しました。私のデータには 30 の異なるレベルがあり、その結果、30 の異なる画像ファイルが作成されました。ここで、これらの png を 1 つのプロットに結合したいと思います。

次のように表示します。

ここに画像の説明を入力

次の例は、私がやりたいことを示しています。

library(rgl)
library(png)
library(gridExtra)
library(ggplot2)

## creates a png in the working directory which can be used as an example
example(surface3d)
rgl.snapshot("example.png")
rgl.close()

## imports the png files; in the example, the same file is imported multiple times.
if(exists("png.df")) rm(png.df)
for (i in 1:9) {
  png.i <- readPNG("example.png")

  g <- rasterGrob(png.i, interpolate=TRUE)
  g <- g$raster
  g <- as.vector(g)
  g <- matrix(g, nrow = 256, ncol = 256, dimnames = list(1:256, 1:256))

  df.i <- data.frame(i = rep(row.names(g), dim(g)[2]), j = rep(colnames(g), each = dim(g)[1]), col=as.vector(g))
  df.i$i <- as.numeric(as.character(df.i$i))
  df.i$j <- as.numeric(as.character(df.i$j))
  df.i$col <- as.character(df.i$col)
  df.i$title <- paste ( "Plot", i)

  if(exists("png.df")) {
    png.df <- rbind(png.df, df.i)
  } else {
    png.df <- df.i
  }
}
rm(df.i, g)

## plots the data
pl <- ggplot(png.df, aes( x = i, y = j))
pl <- pl + geom_raster(aes(fill = col)) + scale_fill_identity()
pl <- pl + scale_y_reverse()
pl <- pl + facet_wrap( ~ title)
pl <- pl + coord_equal() + theme_bw() + theme(panel.grid = element_blank(), axis.text = element_blank(), axis.title = element_blank(), axis.ticks= element_blank())
pl

これはかなりうまく機能しますが、かなり遅いです。実際の png の解像度ははるかに高く、9 個だけでなく 30 個の png をプロットしたいので、マシンが長時間完全に応答しなくなります (i7、8GB RAM)。

インポート部分はかなりうまく機能しますが、結果のデータ フレームは非常に大きく (4.5e+07 行)、ggplot は (当然のことながら) 適切に処理できません。

プロットを迅速かつ効率的に作成するにはどうすればよいでしょうか? できれば R を使用しますが、他のソフトウェアを使用することもできます。

4

1 に答える 1

8

これはgrid関数grid.rasterを使用し、 latticexyplotからのソリューションです。画面へのレンダリングが速いと思うので、パフォーマンスの良い候補です。パネルのカスタマイズを使用してグリッド機能を統合しやすいため、ラティスを選択しています。grid.raster

ここに画像の説明を入力

readPNG最初に、パッケージから使用してすべてのpngを読み取りpngます(ソリューションに似ています)

ll <- list.files(path='c:/temp',patt='compo[0-9].*',full.names=T)
library(png)
imgs <- lapply(ll,function(x){
       as.raster(readPNG(x))  ## no need to convert to a matrix here!
   })

次に、散布図用のデータを準備します。

x = 1:4   ## here 4 because I use  16 plots
y = 1:4
dat <- expand.grid(x,y)

xyplot最後に、カスタム パネル関数を使用します。

library(lattice)
library(grid)
xyplot(Var2~Var1|rownames(dat),data=dat,layout=c(4,4),
      panel=function(x,y,...){
        lims <- current.panel.limits()
        grid.raster(image =imgs[[panel.number()]],sum(lims$xlim)/2,sum(lims$ylim)/2,
                                      width =diff(lims$xlim),
                                          height=diff(lims$ylim),def='native' )

       })

PS:それは私がキャティソリューションと呼んでいるものです。

于 2013-03-05T16:48:15.950 に答える