我看到两个可能互补的替代方案来解决这个问题。第一个是创建一个完全自定义的图例,并将其传递给(主要基于)的key
论点。这是一个示例,以澄清我的观点。xyplot
lsmip
?lsmip
## default trellis point theme
trellis_points <- trellis.par.get("superpose.symbol")
## create customized key
key <- list(title = "Some legend title", # legend title
cex.title = 1.2,
x = .7, y = .9, # legend position
points = list(col = trellis_points$col[1:2], # points
pch = trellis_points$pch[1:2],
cex = 1.5),
text = list(c("A", "B"), cex = .9)) # text
## create results and extract lattice plot
d <- lsmip(warp.lm, wool ~ tension, plotit = FALSE,
main = "Some figure title", key = key)
p <- attr(d, "lattice")
p
如您所见,设置自定义图例可让您修改图例的所有不同组成部分 - 包括标签、文本和符号大小、图例间距等。更深入地了解其中描述的key
参数,?xyplot
其中描述了各种修改选项细节。
现在,如果您有一个较长的图例标题并且您不想将图例包含在绘图区域内,您还可以定义单独的视口,从而允许图例在右边距占据更多空间。请注意使用网格功能从单个图形组件中update
删除最初创建的图例以及随后的组装。p
## remove legend from figure
p <- update(p, legend = NULL)
## assemble figure incl. legend
library(grid)
png("plot.png", width = 14, height = 10, units = "cm", res = 300)
grid.newpage()
## add figure without legend
vp0 <- viewport(x = 0, y = 0, width = .75, height = 1,
just = c("left", "bottom"))
pushViewport(vp0)
print(p, newpage = FALSE)
## add legend
upViewport(0)
vp1 <- viewport(x = .7, y = 0, width = .3, height = 1,
just = c("left", "bottom"))
pushViewport(vp1)
draw.key(key, draw = TRUE)
dev.off()