2014-01-09 10 views
8

Gibt es Bibliotheken oder Funktionen in R, die die Erstellung eines Kometplots vereinfachen? Darunter verstehe ich einen animierten Graph, bei dem der Kopf (ein Pfeil) den Weg eines Teilchens verfolgt und sein Körper (eine Linie) hinter dem Kopf verläuft, mit einer Länge proportional zur Geschwindigkeit des Teilchens.Erzeugen eines animierten Kometplots in R

Matlab hat eine sehr einfacher Komet() Funktion hier dokumentiert: http://www.mathworks.co.uk/help/matlab/ref/comet.html

Ein unglaublich schönes Beispiel eines Kometen Grundstück aktuelle Windgeschwindigkeiten zeigt, ist hier: http://earth.nullschool.net/

Ich weiß, das eine sehr allgemeine Frage ist, , aber ich habe bisher absolut nichts in R gefunden.

+0

I don glaube nicht, dass es so etwas für R gibt. Vielleicht JavaScript oder jQuery? –

+1

Dies kann mit 'D3.js' gemacht werden, werfen Sie einen Blick auf die Beispiele auf der' D3.js' Website. –

Antwort

14

Wie wäre dies mit animation

# required libraries 
require(animation) # NB you must install ImageMagick 
require(ggplot2) 
require(grid) # for arrow() 

# create a path with variable speed (1 point per time unit) 
path<-data.frame(time=1:100,x=1:100,y=c((1:50)^2,(50:1)^2)) 
# plot it, just to see 
ggplot(path)+geom_point(aes(x,y)) 

enter image description here

# work out the path and previous points of each observation 
# average of previous 2 steps, but could be any number 
steps<-2 
path$prevx<-c(rep(0,steps),path$x[1:(nrow(path)-steps)]) 
path$prevy<-c(rep(0,steps),path$y[1:(nrow(path)-steps)]) 
path$prevtime<-c(rep(0,steps),path$time[1:(nrow(path)-steps)]) 
# then calculate the velocity at each point, and the angle (for the arrow) 
# note we could just plot point to point, but that assumes the time units are regular 
path$v<-((((path$x-path$prevx)^2)+((path$y-path$prevy)^2))^0.5)/(path$time-path$prevtime) 
path$atan2<-atan2((path$x-path$prevx),(path$y-path$prevy)) 

# OK, we now have all the data; let's plot first without animation 
ggplot(path)+geom_segment(aes(x = x-(v*sin(atan2)), y = y-(v*cos(atan2)), xend = x, yend = y), 
          arrow=arrow(length = unit(0.5, "cm")) , 
          alpha=0.5, size=2, 
          color="blue") 

enter image description here

# create function which takes a vector of rows (to plot a subset of arrows) 
plot_arrow<-function(vec){ 
    alphas<-rev(1/(1:length(vec))^1.5) # this create an alpha scale 
    g<-ggplot(path[vec,])+geom_segment(aes(x = x-(v*sin(atan2)), y = y-(v*cos(atan2)), xend = x, yend = y), 
           arrow=arrow(length = unit(0.5, "cm")) , # create arrow 
           alpha=alphas, size=2, 
           color="blue")+ 
    coord_cartesian(xlim=c(min(path$x),max(path$x)),ylim=c(min(path$y),max(path$y))) # fix width 
    print(g) 
} 

# then create the animated GIF with 5 arrows per frame 
saveGIF({ 
    lapply(1:nrow(path),function(x)plot_arrow(max(1,x-5):x)) 
},movie.name="comet.gif",interval=0.2) 

enter image description here

PS: Sie können auch die Hinterpfeilspitzen, entfernen, indem die plot_arrow() Funktionsaufruf wie folgt zu ändern [die arrow() Aufruf in 'geom_segment']:

arrow=arrow(length = unit(c(rep(0,length(vec)-1),0.5), "cm")) 

enter image description here

9

Sie können mit dem ausgezeichneten animation Paket spielen.

Vielleicht sollten Sie die points Aufruf an Segmente ändern (oder rufen Sie sie zusammen), aber das sollte ein guter Anfang sein.

enter image description here

library(animation) 
saveGIF({ 
    ani.options(interval = 0.2, nmax = 10) 
    t = seq(0,2*pi,.01) 
    x = cos(2*t)*(cos(t)^2) 
    y = sin(2*t)*(sin(t)^2) 
    idx = seq(1,length(x),10) 
    for (i in seq_along(idx)) { 
    plot(x,y,type='n') 
    points(x[seq(idx[i])],y[seq(idx[i])],pch=20,col='red') 
    ani.pause() ## pause for a while ('interval') 
    } 
}, interval = 0.05, movie.name = "comet_demo.gif", 
     outdir = "/usr/temp",ani.width = 600, ani.height = 600) 
Verwandte Themen