如何将“图片”对象用作图表键中的符号?

时间:2017-02-22 05:33:33

标签: r lattice grimport

我正在使用格子中的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))
)

但事实并非如此。

所以我的问题是:如何将图片对象传递给'key'参数,将其用作密钥中的符号?

1 个答案:

答案 0 :(得分:0)

据我所知,如果不修改代码,就无法将图片从grImport传递到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")))

Imgur