【发布时间】:2015-01-20 09:50:54
【问题描述】:
有什么方法可以在 R 中创建自定义点吗?我熟悉pch 参数,其中有很多选择,但是如果我需要绘制例如树剪影怎么办?
例如,如果我将某个点绘制为 eps。 (或类似)文件,我可以在 R 中使用它吗?。在复杂对象(例如树)的情况下,光栅解决方案并不好。
【问题讨论】:
有什么方法可以在 R 中创建自定义点吗?我熟悉pch 参数,其中有很多选择,但是如果我需要绘制例如树剪影怎么办?
例如,如果我将某个点绘制为 eps。 (或类似)文件,我可以在 R 中使用它吗?。在复杂对象(例如树)的情况下,光栅解决方案并不好。
【问题讨论】:
您可以使用grImport 包来执行此操作。我在 Inkscape 中画了一个螺旋并将其保存为drawing.ps。按照 grImport vignette 中概述的步骤,我们跟踪文件并将其作为一种多边形读取。
setwd('~/R/')
library(grImport)
library(lattice)
PostScriptTrace("drawing.ps") # creates .xml in the working directory
spiral <- readPicture("drawing.ps.xml")
小插图使用格子来绘制符号。您也可以使用基本图形,但需要从设备到绘图坐标的转换。
# generate random data
x = runif(n = 10, min = 1, max = 10)
y = runif(n = 10, min = 1, max = 10)
# lattice (as in the vignette)
x11()
xyplot(y~x,
xlab = "x", ylab = "y",
panel = function(x, y) {
grid.symbols(spiral, x, y, units = "native", size = unit(10, "mm"))
})
# base graphics
x11()
plot(x, y, pty = 's', type = 'n', xlim = c(0, 10), ylim = c(0, 10))
xx = grconvertX(x = x, from = 'user', to = 'ndc')
yy = grconvertY(y = y, from = 'user', to = 'ndc')
grid.symbols(spiral, x = xx, y = yy, size = 0.05)
【讨论】: