2011-03-22 16 views
5

背景:我正在嘗試使用R的lattice :: cloud()函數爲旋轉的3D散點圖創建動畫。我在R中創建一個PNG圖像序列,然後從序列創建一個動畫GIF。如何保存旋轉格子云散射圖的尺度?

問題是,繪圖的比例隨着旋轉而變化,所以動畫立方體在轉動時看起來會增長和縮小。

下面是僅使用兩個地塊(基於雲的例子之一)的一個例子:

library(lattice) 
par.set <- 
    list(axis.line = list(col = "transparent"), 
    clip = list(panel = "off")) 

print(cloud(Sepal.Length ~ Petal.Length * Petal.Width, 
    data = iris, cex = .8, 
    groups = Species, 
    screen = list(z = 0, x = 0, y = 0), 
    par.settings = par.set, 
    scales = list(col = "black")), 
    split = c(1,1,2,1), more = TRUE) 
print(cloud(Sepal.Length ~ Petal.Length * Petal.Width, 
    data = iris, cex = .8, 
    groups = Species, 
    screen = list(z = 0, x = 0, y = 30), 
    par.settings = par.set, 
    scales = list(col = "black")), 
    split = c(2,1,2,1)) 

我想右邊圖顯示的尺寸與左側的情節一樣。在這個例子中它比較小。我不必使用R或格::雲(),但它非常接近我想要的...

嘗試rgl,看起來也許我的rgl副本與我的R版本不兼容。我會升級R'當我有一分鐘:

> library(rgl) 
> df <- data.frame(x=runif(10,0,1), y=runif(10,0,1), z=runif(10,0,1), color=round(runif(10,1,3))) 
> plot3d(df$x, df$y, df$z, col=df$color, size=2, type='s') 
> rgl.snapshot("C:\\pic.png", fmt="png", top=TRUE) 
[1] "failed" 
Warning messages: 
1: In rgl.snapshot("C:\\pic.png", fmt = "png", top = TRUE) : 
    RGL: PNG Pixmap Saver Warning: Application was compiled with png.h from libpng-1.2.40 
2: In rgl.snapshot("C:\\pic.png", fmt = "png", top = TRUE) : 
    RGL: PNG Pixmap Saver Warning: Application is running with png.c from libpng-1.4.1 
3: In rgl.snapshot("C:\\pic.png", fmt = "png", top = TRUE) : 
    RGL: PNG Pixmap Saver Warning: Incompatible libpng version in application and library 

> sessionInfo() 
R version 2.11.1 (2010-05-31) 
i386-pc-mingw32 

locale: 
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 
[4] LC_NUMERIC=C       LC_TIME=English_United States.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] rgl_0.92.798 

loaded via a namespace (and not attached): 
[1] tools_2.11.1 

我更新至R 2.12.2,現在這工作(使用的替代ImageMagick GraphicsMagick工具):

open3d() 
with(iris, plot3d(Petal.Length, Petal.Width, Sepal.Length, col=Species)) 
movie3d(spin3d(), duration=12, dir="C:\\Movie", convert=FALSE) 
system('"C:\\program files\\graphicsmagick-1.3.7-q8\\gm.exe" convert -delay 12 C:\\Movie\\movie*.png C:\\Movie\\animate.gif') 

回答

3

嘗試RGL包,這個是我如何接近相同的現象

require ("rgl") 

df <- data.frame(x=runif(10,0,1), y=runif(10,0,1), z=runif(10,0,1), color=round(runif(10,1,3))) 

plot3d(df$x, df$y, df$z, col=df$color, size=2, type='s') 

?movie3d() 

demo(flag) 

test <- spin3d(rpm=6,axis=c(0,0,1)) 
?spin3d 

open3d() 
plot3d(oh3d(col="lightblue", alpha=0.5)) 
play3d(spin3d(axis=c(0,0,1), rpm=20), duration=3) 


open3d() 
plot3d(cube3d(col="green")) 
M <- par3d("userMatrix") 
play3d(par3dinterp(userMatrix=list(M, 
           rotate3d(M, pi/2, 1, 0, 0), 
           rotate3d(M, pi/2, 0, 1, 0))), 
    duration=4) 

movie3d(spin3d(), duration=5) 

我已確認的工作示例的會話信息。

R version 2.12.2 (2011-02-25) 
Platform: i386-pc-mingw32/i386 (32-bit) 

locale: 
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252 
[3] LC_MONETARY=English_United States.1252 
[4] LC_NUMERIC=C       
[5] LC_TIME=English_United States.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] rgl_0.92.798 

loaded via a namespace (and not attached): 
[1] tools_2.12.2 
+0

@OneWhoIsUnnamed這看起來很有希望,但由於某些原因,movie3d()無法創建輸出文件。它爲每個幀創建空的png文件,並記錄一個錯誤,如「Writing movie115.png [1]」failed「。 – 2011-03-22 23:29:12

+0

另外plot3d()似乎對cloud()的外觀沒有相同的控制級別。如何更改默認窗口大小和軸標籤的大小?謝謝! – 2011-03-22 23:31:03

+0

@Kent Johnson我不確定爲什麼movie3d()會失敗,上面的工作在我的機器上沒有錯誤,但是授予的不是您正在尋找的東西,而是一個說明性示例。爲了排除故障,你可以發佈sessionInfo()的輸出嗎?也許這是一個R版本的問題。也可以發佈一個你用來得到這個錯誤的最小命令的例子嗎?嘗試複製此錯誤對我來說很有用。 – 2011-03-23 13:40:43