【问题标题】:How do I use a 'picture' object as a symbol in a graph key?如何使用“图片”对象作为图形键中的符号?
【发布时间】:2017-02-22 05:33:33
【问题描述】:

我正在使用 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))
)

但事实并非如此。

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

【问题讨论】:

    标签: r lattice grimport


    【解决方案1】:

    据我所知,在不修改代码的情况下,无法将您的图片从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")))
    

    【讨论】:

      猜你喜欢
      • 2020-08-03
      • 2021-07-10
      • 2020-03-25
      • 2020-10-12
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 2013-05-01
      • 2018-05-15
      相关资源
      最近更新 更多