lunes, 20 de diciembre de 2010

Feliz Navidad con R

Se acerca la navidad, se finaliza año y las personas van reuniéndose con sus familiares para celebrar estas fechas. Por lo cual Bitácoras en Estadística no quiere dejar pasar esta fecha sin dar a sus lectores una feliz navidad con R. 

Por cierto este es el código con el cual se realizo la animación en R:


f.x = c(0.193,0.703,0.703,0.295,0.295,0.703,0.703,0.295,0.295,0.193,0.193)
f.y = c(0.935,0.935,0.835,0.835,0.575,0.575,0.475,0.475,0.063,0.063,0.935)
e.x = c(0.222, 0.787, 0.787, 0.324, 0.324, 0.757, 0.757, 0.324, 0.324,0.804, 0.804, 0.222)
e.y = c(0.935, 0.935, 0.834, 0.834, 0.564, 0.564, 0.464, 0.464, 0.163,0.163, 0.063, 0.063)
l.x = c(0.222,0.324,0.324,0.804,0.804,0.222)
l.y = c(0.935,0.935,0.163,0.163,0.063,0.063)
a1.x = c(0.433, 0.546, 0.865, 0.746, 0.656, 0.328, 0.242, 0.136)
a1.y = c(0.935, 0.935, 0.063, 0.063, 0.326, 0.326, 0.063, 0.063)
a2.x = c(0.488, 0.629, 0.355)
a2.y = c(0.841, 0.418, 0.418)
n.x = c(0.189, 0.295, 0.707, 0.707, 0.804, 0.804, 0.701, 0.287, 0.287, 0.189)
n.y = c(0.935, 0.935, 0.248, 0.935, 0.935, 0.063, 0.063, 0.747, 0.063, 0.063)
d.x = c(0.218,0.56,0.56,0.56,0.56,0.32,0.218,0.32,0.32)
d.y = c(0.935,0.933,0.832,0.518,0.418,0.063,0.063,0.518,0.834)
d.cir.th = seq(pi/2, -pi/2, length.out = 50)
d1.cir.x = 0.32  + 0.515 * cos(d.cir.th)
d1.cir.y = 0.499 + 0.436 * sin(d.cir.th)
d2.cir.x = 0.32 + 0.4147 * cos(d.cir.th)
d2.cir.y = 0.499 + 0.3 * sin(d.cir.th)
d1.x = c(0.218, d1.cir.x, 0.32, 0.218)
d1.y = c(0.935, d1.cir.y, 0.063, 0.063)
d2.x = c(0.32, d2.cir.x, 0.32)
d2.y = c(0.799, d2.cir.y,0.199)
n.x = c(0.189, 0.295, 0.707, 0.707, 0.804, 0.804, 0.701, 0.287, 0.287, 0.189)
n.y = c(0.935, 0.935, 0.248, 0.935, 0.935, 0.063, 0.063, 0.747, 0.063, 0.063)
i.x = c(0.189, 0.295, 0.295, 0.189, 0.189)
i.y = c(0.935, 0.935, 0.063, 0.063, 0.935)
e.x = c(0.222, 0.787, 0.787, 0.324, 0.324, 0.757, 0.757, 0.324, 0.324,0.804, 0.804, 0.222)
e.y = c(0.935, 0.935, 0.834, 0.834, 0.564, 0.564, 0.464, 0.464, 0.163,0.163, 0.063, 0.063)
z.x = c(0.222,0.787,0.787,0.322,0.804,0.804,0.222,0.222,0.687,0.222)
z.y = c(0.935,0.935,0.834,0.163,0.163,0.063,0.063,0.163,0.834,0.834)
v.x = c(0.136, 0.259, 0.496, 0.734, 0.853, 0.543, 0.441)
v.y = c(0.935, 0.935, 0.163, 0.935, 0.935, 0.063, 0.063)
draw.ch = function(x1, y1, x2 = NULL, y2 = NULL, center.x,
    color, alpha, xscale) {
    rgb.col = col2rgb(color)/255
    x1 = x1 * xscale + center.x - 0.5 * xscale
    x2 = x2 * xscale + center.x - 0.5 * xscale
    polygon(x1, y1, col = rgb(rgb.col[1], rgb.col[2], rgb.col[3],
        alpha = alpha), border = NA)
    polygon(x2, y2, col = "black", border = NA)
}
x1 = list(f.x, e.x, l.x, i.x, z.x, n.x, a1.x,v.x,i.x,d1.x,a1.x,d1.x)
y1 = list(f.y,e.y,l.y,i.y,z.y,n.y,a1.y,v.y,i.y,d1.y,a1.y,d1.y)
x2 = list(NULL,NULL,NULL,NULL,NULL,NULL,a2.x,NULL,NULL,d2.x,a2.x,d2.x)
y2 = list(NULL,NULL,NULL,NULL,NULL,NULL,a2.y,NULL,NULL,d2.y,a2.y,d2.y)
th = seq(pi/6, 2 * pi, length.out = 12)
cols = rainbow(200)
library(animation)
saveMovie({
for (j in 1:238) {
        th = th - pi/120
        center.x = 3 + 5 * cos(th)
        cols = c(cols[-1], cols[1])
        alpha = 0.1 + (50 * (1 - sin(th)))/100
        alpha = ifelse(alpha > 1, 1, alpha)
        xscale = -sin(th) * 1.2
        plot(1, xlim = c(-2, 8), ylim = c(-2.5, 3.5), type = "n")
        plot.order = (1:12)[order(xscale > 0)]
        for (k in 1:12) {
            i = plot.order[k]
            draw.ch(x1[[i]], y1[[i]], x2[[i]], y2[[i]], center.x[i],
                color = cols[k + 60 * ( i >= 6 & i <= 8 ) + 120 *
                    (i > 8)], alpha[i], xscale[i])
        }
        text(8, -2.5, "Bitácoras en Estadística (http://experienceinstatistics.blogspot.com)",
            adj = c(1, 0), col = "white", cex = 0.8)

    }
}, interval = 0.05,para = list(mar = rep(0,4), bg = "black"), width = 640, height = 320)

Esto se puede realizar con el paquete animation. Gracias Yihui Xie.

Ver también: Nuevo año 2010 con R.