2013-02-13 10 views
7

Ich möchte die folgende Art von Handlung erstellen. Aber ich weiß nicht ob es schon ein Paket gibt, da ich keins finden konnte. Daten:Zeitanzeige in Uhr mit xy Streudiagramm in r

myd <- data.frame (X = 1:5, Y = c(0.8, 0.6, 0.7, 0.75, 0.1), 
    clockd = c(12.05, 12.25, 12.45, 1.30, 2.1)) 

clockd sind Zeit 12.05 ist 12 Oclock letzten Minuten. Ich weiß nicht, geeignete Zeiteinheit

plot (myd$X, myd$Y) 

bearbeiten in R. eingeben: Die roten und grünen Pfeil stellt die Stunde und minitue Position der Takt Arme in einer Uhr. enter image description here

+1

Vielleicht können Sie die Länge der Pfeile soll darstellen erklären, was. Ich vermute, X und Y repräsentieren die Position der Uhr in Ihrer Handlung? – plannapus

+0

Vielleicht könnten Sie dieses Stück Code anpassen? http://stackoverflow.com/questions/11877379/how-to-draw-clock-in-r –

+0

@plannapus X und Y ist die Position der Uhr im Feld, während der Pfeil Stunde und Minute Arm einer Uhr – rdorlearn

Antwort

6
clockplot<-function(x, y, h, m, r, border="black", col="lightblue", 
        col.hour="darkblue", col.min="red"){ 
#x and y are the coordinates of the clock 
#h and m the hour (base 12) and minutes 
# r the radius of the clock 
    t<-seq(0,2*pi,by=0.01) 
    x.c<-r*cos(t)+x 
    y.c<-r*sin(t)+y 

    t.h<-pi/2 - 2*pi*(h-m/60)/12 
    x.h<-0.5*r*cos(t.h)+x 
    y.h<-0.5*r*sin(t.h)+y 

    t.m<-pi/2 - 2*pi*m/60 
    x.m<-r*cos(t.m)+x 
    y.m<-r*sin(t.m)+y 

    polygon(x.c,y.c,col=col, border=border) 
    segments(x,y,x.h,y.h,col=col.hour) 
    segments(x,y,x.m,y.m,col=col.min) 
    } 

myd <- data.frame (X = 1:5, Y = c(0.8, 0.6, 0.7, 0.75, 0.1), 
        clockd = c(12.05, 12.25, 12.45, 1.30, 2.1)) 
myd$hour<-myd$clockd%/%1 
myd$min<-myd$clockd%%1 *100 

plot(myd$X, myd$Y, type="l", asp=1) 
apply(myd,1,function(x)clockplot(x[1],x[2],x[4],x[5], r=0.25)) 

enter image description here

4

enter image description here

Geben Sie Ihre Daten:

read.table(text = 'col1 col2 
      0012 0001245',head=T,colClasses=c('character','numeric')) 


myd <- data.frame (X = 1:5, 
        Y = c(0.8, 0.6, 0.7, 0.75, 0.1), 
        clockd = c(12.05, 12.25, 12.45, 1.30, 2.1)) 

Mit Gitter + Gitter können wir some wie folgt tun:

library(lattice) 
library(grid) 
xyplot(Y~X,data=myd, 
     panel=function(x,y,...) 
     { 

     panel.fill(col='yellow') 
     panel.lines(x,y) 

     h_min <- do.call(rbind,strsplit(as.character(myd$clockd),'[.]')) 
     hours <- as.numeric(h_min[,1]) 
     minutes <- as.numeric(h_min[,2]) 
     lapply(seq_along(x), 
         function(i)drawClock(x[i],y[i],hour = hours[i], minute = minutes[i])) 


     }) 

Wo drawClock von this und trsnformed zu der Annahme Notwendigkeit dieser Frage:

drawClock <- function(x0,y0,hour, minute) { 
    t <- seq(0, 2*pi, length=13)[-13] 
    x <- cos(t) 
    y <- sin(t) 
    # Circle with ticks 
    grid.circle(x=x0, y=y0, default="native", 
       r=unit(0.4,'in'),gp=gpar(fill='blue',alpha=0.5)) 
    #   grid.segments(x, y, x*.9, y*.9, default="native") 
    # Hour hand 
    hourAngle <- pi/2 - (hour + minute/60)/12*2*pi 
    grid.segments(x0, y0, 
       x0+.06*cos(hourAngle), y0+.06*sin(hourAngle), 
       default="native", gp=gpar(lex=4)) 
    # Minute hand 
    minuteAngle <- pi/2 - (minute)/60*2*pi 
    grid.segments(x0, y0, 
       x0+.08*cos(minuteAngle), y0+.08*sin(minuteAngle), 
       default="native", gp=gpar(lex=2))  
} 
2

Blick auf die my.symbols Funktion im TeachingDemos Paket für Werkzeuge dieser Art von Handlung helfen zu erstellen.

5

Ich verwende einen Vorschlag von @GregSnow hier mit my.symbols Funktion von TeachingDemo Paket. Auf diese Weise können Sie es mit einem kleinen (ish) Stück Code tun, aber Sie können ziemlich große Kontrolle über die grafischen Parameter wie Größe und Füllung von Uhrkreisen, Aussehen von Pfeilen, die Art, wie Sie Ihre Handlung anpassen, usw. berechnen von Pfeilen habe ich den @agstudy-Code so geändert, dass er die Minuten korrekt extrahiert.

myd <- data.frame (X = 1:5, Y = c(0.8, 0.6, 0.7, 0.75, 0.1), 
        clockd = c(12.05, 12.25, 12.45, 1.30, 2.1)) 
hour <- round(myd$clockd)#takes hours by ignoring decimals 
minute <- 100*(myd$clockd - trunc(myd$clockd,2))#takes decimals 
#for getting the angle I'm subtracting from pi/2 
#thats because pi/2 orients the arrow into 0 degree position, pointing up 
hourAngle <- pi/2 - (hour/12*2*pi) 
minuteAngle <- pi/2 - (minute/60*2*pi) 
#now all the plotting 
plot(myd$X, myd$Y, type="l", xaxt="n", xlab="", ylab="", 
    xlim=c(0.5,5.5), ylim=c(0,1), col="gray")#standard plot, no x axis 
axis(1, at=myd$X, labels=myd$X)#custom x-axis 
require(TeachingDemo) 
my.symbols(myd$X, myd$Y, ms.arrows, angle=hourAngle, add=T, 
      col="blue", symb.plots=TRUE, adj=0) 
my.symbols(myd$X, myd$Y, ms.arrows, angle=minuteAngle, add=T, 
      col="red", symb.plots=TRUE, adj=0) 
my.symbols(myd$X, myd$Y, ms.polygon, n=250, add=T, 
      r=1.1, col="gray") 

enter image description here