如何有效地将多个rgl图合并为一个图? [英] How to join efficiently multiple rgl plots into one single plot?

查看:100
本文介绍了如何有效地将多个rgl图合并为一个图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用rgl包为数据的每个因子水平制作了3D图,并将它们另存为png.我的数据有30个不同的级别,因此产生了30个不同的图像文件.现在,我想将这些png合并为一个图.

I produced 3D plots with the rgl package for each factor level of my data and saved them as pngs. My data has 30 different levels, which resulted in 30 different image files. Now I would like to combine these pngs into one single plot.

我会像这样显示它们:

以下示例说明了我想做的事情:

The following example illustrates what I would like to do:

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具有更高的分辨率,我想绘制30 png,而不仅仅是9个,这导致我的机器在相当长的时间内完全不响应(i7,8GB RAM).

This works rather well, but it is quite slow. The real pngs have a much higher resolution, and I'd like to plot 30 pngs, not just 9, which results in my machine being totally unresponsive for quite a long time (i7, 8GB RAM).

导入部分可以正常工作,但是结果数据帧非常大(4.5e + 07行),ggplot(可以理解)无法正确处理.

The importation part works reasonably well, but the resulting data frame is extremely big (4.5e+07 rows), which ggplot (understandably) can't handle properly.

如何快速有效地创建情节?最好使用R,但也可以使用其他软件.

How could a plot be created in a fast and efficient manner? Preferably with R, but other software might also be used.

推荐答案

以下是使用格子中的 grid 函数grid.rasterxyplot的解决方案. 我认为grid.raster具有更快的速度 渲染到屏幕上,因此它是性能的理想选择.我之所以选择晶格,是因为它使用面板自定义功能更容易集成网格功能.

Here's a solution using the grid functiongrid.raster and xyplot from lattice. I think that grid.raster has faster rendering to screen, so it is a good candidate for performance. I am choosing lattice because it integrate easier the grid function using the panel customization.

首先,我使用png包中的readPNG读取了所有png(类似于您的解决方案)

First I read all the png, using readPNG from png package ( similar to your solution)

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!
   })

然后我准备散点图的数据:

Then I prepare data for scatter plot:

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

最后,我将xyplot与自定义面板功能一起使用:

Finaly I use xyplot with a custom panel function:

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:这就是我所说的 catty 解决方案.

PS: it is what I call a catty solution.

这篇关于如何有效地将多个rgl图合并为一个图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆