提供您的数据:
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))
使用 lattice+grid 我们可以这样做:
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]))
})
从这里采用 drawClock 并根据这个问题的需要进行转换:
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))
}