如何在R中绘制时钟? [英] How to draw clock in R?

查看:126
本文介绍了如何在R中绘制时钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究R,并尝试使用饼图绘制时钟.

I am working on R and trying to draw a clock using a pie chart.

代码:

pie(c(25,20,15,10,10,30),
    labels = c(1,2,3,4,5,6,7,8,9,10,11,12),
    col = rainbow(length(lbls)), clockwise = TRUE, init.angle = 90)

但是我需要所有12个标签都在那里,而与输入中没有任何段无关.

but i need all 12 labels to be there independent of no of segments in input.

我该如何实施?

致谢

推荐答案

Paul Murrell在他的最后一本书中(

Paul Murrell in his last book (R graphics 2nd Edition) have some code for drawing an interactive clock using grid and gwidget.

以下是用于绘制简单的非交互式时钟的示例代码:

Here is a sample code for drawing a simple non interactive clock :

require(grid)

drawClock <- function(hour, minute) {
    t <- seq(0, 2*pi, length=13)[-13]
    x <- cos(t)
    y <- sin(t)

    grid.newpage()
    pushViewport(dataViewport(x, y, gp=gpar(lwd=4)))
    # Circle with ticks
    grid.circle(x=0, y=0, default="native", 
                r=unit(1, "native"))
    grid.segments(x, y, x*.9, y*.9, default="native")
    # Hour hand
    hourAngle <- pi/2 - (hour + minute/60)/12*2*pi
    grid.segments(0, 0, 
                  .6*cos(hourAngle), .6*sin(hourAngle), 
                  default="native", gp=gpar(lex=4))
    # Minute hand
    minuteAngle <- pi/2 - (minute)/60*2*pi
    grid.segments(0, 0, 
                  .8*cos(minuteAngle), .8*sin(minuteAngle), 
                  default="native", gp=gpar(lex=2))    
    grid.circle(0, 0, default="native", r=unit(1, "mm"),
                gp=gpar(fill="white"))
}

现在您可以像这样尝试

drawClock(hour = 2, minute = 30)

完整代码 此处

这篇关于如何在R中绘制时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆