【问题标题】:(R) How to put two motion plots together in R, and save them as a Gif file?(R) 如何在 R 中将两个运动图放在一起,并保存为 Gif 文件?
【发布时间】:2014-04-29 20:16:22
【问题描述】:

我正在通过年轮测量来真实地重现树木的年度生长情况,但我在处理图表及其边距时遇到了很多问题。我已经分别完成了这两个图表,它们工作正常,但目前将它们放在一起,它不起作用!当我想保存为 GIF 文件时,就更少了。

这里分别是我的两个运动图的代码:

x = tree01$x
y = tree01$y
z = tree01$z

year=tree01$year
TRW = tree01$TRW

# PLOTTING DISC
plot.new()
for(i in 1:125) {
  Sys.sleep(0.1)
  par(new=T)
  plot(x[1],y[1], xlim = c(-1000, 1000), ylim = c(-10000, 10000))
  par(new=T)
  points(x[i],y[i], cex= z[i], col="black")
  legend("topright", legend= year[i], border="") # I tried to draw without border
}

# PLOTTING LINES
plot.new()

for (i in 1:125){
  Sys.sleep(0.1)
  plot(x=year, y=TRW, type="l", ylim = c(0,5)) 
  points(year[i], TRW[i], col="red", pch=20, font = 20)
  abline(v=year[i], col = "grey")
}

这就是为什么 a 试图将它们放在一起,但在不同的面板中,但不幸的是不起作用:

library(animation)

# saveGIF({

for(i in 1:125) {
  old.par <- par(mfrow=c(2,1))
  #disc
  Sys.sleep(0.1)
  par(new=T)
  plot(x[1],y[1], xlim = c(-10000, 10000), ylim = c(-10000, 10000))
  par(new=T)
  points(x[i],y[i], cex= z[i], col="black")
  legend("topright", legend= year[i], border="")
  #lines
  par(new=T)
  plot(x=year, y=TRW, type="l", ylim= c(0,500))
  points(year[i], TRW[i], col="red", pch=20, font = 20)
  abline(v=year[i], col = "grey")
  par(old.par)
 }

# }, movie.name = "tree01.gif", interval = 0.1, ani.width = 600, ani.height = 600)

最后我希望有一个像这样的 GIF 文件: ![年树生长][1]

我将非常感谢您的帮助。谢谢!

表格:

year    TRW x   y   z
1884    1,1 -2,75   3,5 0,34
1885    1,17    -2  3,75    0,81
1886    1,14    13,25   -2  1,34
1887    0,84    -7,5    -6,5    1,67
1888    0,51    2,25    -5,5    1,91
1889    0,51    3   4   2,26
1890    1,18    -5,25   0,25    3,05
1891    1,01    -4,75   9,5 3,88
1892    1,35    -11,5   16  5,07
1893    1,27    6,75    3,25    6,21
1894    1,28    -0,75   -7,75   7,39
1895    0,67    -12,25  6   8,07
1896    0,7 -5  2,25    8,82
1897    0,34    12,5    -1,25   9,15
1898    0,45    -3,5    1,75    9,66
1899    2,72    -5,25   -1  12,59
1900    1,5 -14,75  -13 14,23
1901    1,27    -13,5   9,5 15,7
1902    1,12    10,25   33,5    17,19
1903    0,28    20,5    6,25    17,54
1904    0,42    23  -4,5    18
1905    0,74    11,25   -4,5    18,86
1906    1,62    -9,25   -14,5   20,63
1907    0,14    -1,25   0,25    20,8
1908    2,02    29  -50,75  23,14
1909    0,22    3,25    0,25    23,45
1910    1,58    5,5 -31,75  25,36
1911    1,33    2,25    -23,25  26,95
1912    0,22    5,25    -3,25   27,25
1913    0,22    5,25    -0,25   27,58
1914    0,39    1,5 2,75    28,19
1915    1,7 79,5    -39,25  30,54
1916    1,57    44  -53,25  32,53
1917    0,55    20  -9,75   33,29
1918    2,33    61,75   -12,5   36,82
1919    0,16    10,5    0,5 37,1
1920    2,05    23,5    -30,25  39,87
1921    1,03    57,5    -2,75   41,47
1922    0,67    57,5    -10,75  42,44
1923    1,24    61  11  44,36
1924    1,13    47  3,25    45,98
1925    0,47    20  20,5    46,84
1926    0,1 -1  34  47,31
1927    0,27    -1  -10 47,52
1928    1,56    -10,5   32  49,88
1929    0,31    -11,5   21,25   50,53
1930    0,65    -20 37,25   51,84
1931    0,84    -15,75  14,75   53,23
1932    1,65    -29 -20,5   55,52
1933    1,3 -54 -29,75  57,34
1934    1,24    -30,75  -26,5   59,04
1935    0,36    -36,5   -3,5    59,66
1936    2,18    -60,25  -27,25  62,82
1937    0,89    -13 11,5    64,24
1938    1   -44 28,5    66,07
1939    2,11    -9  15,5    69,07
1940    0,17    -22 5   69,42
1941    0,6 -55,25  26,25   70,6
1942    0,23    -20,75  5,75    70,99
1943    0,62    -59,25  22  72,12
1944    1,95    -95,75  -5,25   74,39
1945    0,44    -19,5   -8,75   74,75
1946    0,61    -12,25  -9,75   75,15
1947    2,14    -68 14,25   77,69
1948    0,54    -16,25  -4,25   78,07
1949    1,24    -37,25  17,25   79,54
1950    2,46    17,75   -3,75   81,46

【问题讨论】:

  • 不是特定于 R 的解决方案,但 ImageMagick(特别是 convert)对您有用吗?
  • @r2evans 我没试过,但至少我想知道如何像上图那样把两张图放在一起。
  • 两件事:(1)摆脱par(new),它们会导致重叠的情节(pointslegendabline不会导致新的情节); (2) 对于不同的高度,尝试layout(c(1,2), heights=c(3,2)),而不是par(mfrow=c(2,1))(您可能需要使用par(mar))。 layout 通常将矩阵作为其第一个参数,因为对于这么简单的事情,它可以正常工作;见help(layout)
  • 感谢@r2evans 的建议,但我怎样才能在最后像图像中那样得到重叠的环(点)?当我最后摆脱par(new=TRUE) 时,我的图表中只有一个环(最后一个)。
  • 不幸的是,因为您的示例不可重现(数据不可用),我无法检查出来。 (我想我知道你在做什么,但没有数据我看不到自己。)

标签: r graphics graph plot animated-gif


【解决方案1】:

(此答案是旁谈的结果,以使其远离评论部分。)

OP 试图在一个图上交替绘制,然后在另一个图上绘制,这在当前实现中是不可行的。

一些事情:

  • 你不需要par(new)来满足你的需要。

  • 使用cex 准确表示半径是不正确的。请改用symbols(..., circles)

整个数据的一个样本:

tree01 <- structure(list(year = 1884:1893, TRW = c(1.1, 1.17, 1.14,
0.84, 0.51, 0.51, 1.18, 1.01, 1.35, 1.27), x = c(-2.75, -2, 13.25,
-7.5, 2.25, 3, -5.25, -4.75, -11.5, 6.75), y = c(3.5, 3.75, -2, -6.5,
-5.5, 4, 0.25, 9.5, 16, 3.25), z = c(0.34, 0.81, 1.34, 1.67, 1.91,
2.26, 3.05, 3.88, 5.07, 6.21)), .Names = c("year", "TRW", "x", "y",
"z"), row.names = c(NA, 10L), class = "data.frame")

尝试以下方法:

xlim <- range(x-z, x+z)
ylim <- range(y-z, y+z)
opar <- par(c('mfrow','mar'))
layout(c(1,2), heights=c(2,1))
for (i in seq.int(nrow(tree01))) {
    par(mar=c(3,4,4,2)+0.1)
    cols <- c(rep('grey', i-1), 'red')
    symbols(x[1:i], y[1:i], circles=z[1:i], inches=FALSE, xlim = xlim, ylim = ylim, xlab='', ylab='', fg=cols)
    legend('topright', legend=year[i], box.lty=0)
    par(mar=c(5,4,0,2)+0.1)
    plot(year[1:i], y=TRW[1:i], type='l', xlim=c(1884,2008), ylim=c(0,3), xlab='year', ylab='TRW')
    points(year[i], TRW[i], col='red', pch=20, font=20)
    abline(v=year[i], col='grey')
    Sys.sleep(0.1)
}
par(opar)

【讨论】:

  • @r2vans 非常感谢您的回答以及聊天中的所有观察。很高兴找到像你这样的 R 专家和好人。祝你有美好的一天!
猜你喜欢
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
相关资源
最近更新 更多