2017-02-22 33 views
1

我使用grid中的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)) 
) 

但它没有。

所以我的问题是:我如何将图片对象传递给'钥匙'参数使用它作为钥匙中的符号?

回答

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

相关问题