时间显示在时钟与r中的xy散点图

rdo*_*arn 7 plot r ggplot2

我想创建以下类型的情节.但是不知道是否有任何包已经存在,因为我找不到一个.数据:

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))
Run Code Online (Sandbox Code Playgroud)

clockd是时间12.05是12点过去几分钟.我不知道在R中输入时间的合适单位

plot (myd$X, myd$Y)
Run Code Online (Sandbox Code Playgroud)

编辑: 红色和绿色箭头表示时钟中钟表的小时和小时位置. 在此输入图像描述

pla*_*pus 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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Gee*_*cid 5

我正在使用@GregSnow的建议,包含my.symbols函数TeachingDemo.这样你可以使用一小块(ish)代码来完成它,但你可以对图形参数进行很大的控制,比如时钟圆的大小和填充,箭头的外观,你自定义绘图的方式等.用于计算位置我修改了@agstudy代码,以便正确提取分钟数.

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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述