Pregunta

I'm doing a real recreation of the annual tree growth from tree-ring measurements, and I have many problems working with graphs and their margins. I have done the two graphs separately and these work fine, but at the moment to put them together, it doesn't work! and much less when I want to save as GIF files.

Here is the code of my two motion graphs separately:

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

That is why a tried to put them together, but in different panels, but unfortunately doesn't work:

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)

and I would like at the end to have a GIF file like this: ![Annual tree growth][1]

I will be very grateful for your help. Thanks!

TABLE:

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
¿Fue útil?

Solución

(This answer is the result of a side-chat to keep it out of the comment section.)

OP was trying to alternate plotting on one graph then the other, something that isn't feasible in the current implementation.

A few things:

  • You don't need par(new) for what you need.

  • Using cex to accurately represent radius is incorrect. Use symbols(..., circles) instead.

A small sample of the whole data:

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")

Try the following:

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)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top