1

我正在使用 lattice 中的 xyplot() 创建一个图形,并使用 grImport 包将图片绘制为带有 grid.symbols() 的绘图符号。

这是我的图表代码的简短版本,其中“mypic”是我用作绘图符号的图片对象(使用 grImport 创建)。

xyplot(y~x, data=dat,
  panel=function(x, y, ...) {
   grid.symbols(mypic, x, y, units = "native",
    size = unit(10, "mm"),
    use.gc = FALSE,
    gp = gpar(col = "white", fill = c("red","blue")))
})

我想创建一个图例,显示我在图中使用的相同绘图符号。我认为这样的事情会起作用:

key = list(...
  points = list(pch = grid.picture(mypic))
)

但事实并非如此。

所以我的问题是:如何将图片对象传递给“键”参数以将其用作键中的符号?

4

1 回答 1

0

据我所知,如果不修改代码,就无法将您的图片传递给grImportto 。key

我有一个解决方案,虽然它是非常临时的,特别是因为关键移动中的图像然后图像被调整大小。(我认为可以通过一些努力来解决这个问题。)无论如何,使用key(或auto.key)并手动将图像放在键上会得到接近预期结果的结果。

library(lattice)
library(grImport)

dat <- data.frame(x = rnorm(10), y = rnorm(10), ind = c("A", "B"))

PostScriptTrace("petal.ps") # from https://www.jstatsoft.org/article/view/v030i04
petal <- readPicture("petal.ps.xml") 

xyplot(y ~ x, groups = ind, data = dat,
       auto.key = list(points = FALSE),
       panel = function(x, y, ...) {
         grid.symbols(petal, x, y, units = "native",
                      size = unit(4, "mm"),
                      use.gc = FALSE,
                      gp = gpar(col = "white", fill = c("red","blue")))
       })

grid.symbols(petal, c(0.55, 0.55), c(0.92, 0.95), 
             size = unit(4, "mm"),
             use.gc = FALSE,
             gp = gpar(col = "white", fill = c("red","blue")))

伊姆古尔

于 2017-02-22T14:07:01.907 回答