最近两天Friedrich在R-devel发了一个通知,告诉大家要开始准备Google Summer of Code了!然后一位叫Oleg Sklyar的同志迫不及待提出了两个主意,结果讨论很快就乱了套,因为他提的第一个主意就是关于交互式图形的,他说很关键的一点是R不能实现图形的缩放和平移(zooming and panning),我一看,不对劲呀,谁说不能缩放、平移呀,于是三下五除二给了一个例子:
##################################################################
# a demo for zooming and panning in R graphics
# by Yihui Xie, Feb 20, 2009
##################################################################
# a large number of points
plot(x <- rnorm(5000), y <- rnorm(5000), xlab = "x", ylab = "y")
xylim <- c(range(x), range(y))
zoom <- function(d, speed = 0.05) {
rx <- speed * (xylim[2] - xylim[1])
ry <- speed * (xylim[4] - xylim[3])
# global assignment '<<-' here!
xylim <<- xylim + d * c(rx, -rx, ry, -ry)
plot(x, y, xlim = xylim[1:2], ylim = xylim[3:4])
NULL
}
# Key `+`: zoom in; `-`: zoom out
# Left, Right, Up, Down: self-explaining
# `*`: reset
# Press other keys to quit
keybd <- function(key) {
switch(key, `+` = zoom(1), `-` = zoom(-1), Left = zoom(c(-1,
1, 0, 0)), Right = zoom(c(1, -1, 0, 0)), Up = zoom(c(0,
0, 1, -1)), Down = zoom(c(0, 0, -1, 1)), `*` = plot(x,
y), "Quit the program")
}
getGraphicsEvent(onKeybd = keybd)
##################################################################
上图仅限于Windows下操作,因为getGraphicsEvent()函数仅限于Windows图形设备使用。通过键盘上的加(放大)减(缩小)乘(恢复)和上下左右键可以很容易实现图形的动态缩放和平移。
不过后来Simon Urbanek老大发话了,Yihui你这不叫交互式图形;Simon老大说得对,不过俺只是为了说明R并不是像Oleg老大讲的那样不能实现缩放和平移。
赞赏
作为一名没有固定工作的自由职业者,我非常感谢您通过捐赠的方式来支持我的写作和开源软件开发。当然,捐赠纯属自愿。无论金额多少,都是一片诚挚的心意。支付方式如下:
| 微信 | ← 奋力支开它俩 → | 支付宝 |
|---|---|---|
![]() |
其它爱心通道 ↓ Venmo: @yihui_xie Zelle: xie@yihui.name PayPal: xie@yihui.name |
![]() |
若使用 Venmo/Zelle/Paypal,请添加备注“gift”或“donation”,以免捐赠被视为我的可税收入。若使用 Paypal,支付类型请选 Family and Friends,而不要选 Goods and Services。
在不影响生活的前提下,我会将收到的捐赠以尽量大的比例回馈给开源社区和慈善机构。作为参考,2024-25 年间我共收到约三万美元捐赠,完税后我转手捐出了一万五千美元。

