我在网格中使用xyplot()创建图形,并使用grid.symbols()使用grImport包将图片绘制为绘图符号。
这是我的图形代码的一个简短版本,其中'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‘参数,以便将其用作key中的符号?
发布于 2017-02-22 22:07:02
据我所知,在不修改代码的情况下,无法将图片从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")))

https://stackoverflow.com/questions/42383444
复制相似问题