如何更改格子图中的图例标题和位置
How to change the legend title and position in a lattice plot
我正在使用 lsmeans 中的 lsmip
来绘制我的模型,
library(lsmeans)
PhWs1 <- lsmip(GausNugget1, Photoperiod:Ws ~ Month,
ylab = "Observed log(number of leaves)", xlab = "Month",
main = "Interaction between Photoperiod and Water stress over the months (3 photoperiods)",
par.settings = list(fontsize = list(text = 15, points = 10)))
但我无法在互联网上获得有关如何处理图例位置、大小、标题等的建议。
我使用 trellis.par.get()
查看参数,但找不到与我的问题相关的参数。从图中可以看出,图例应该是 "Photoperiod*Ws" 但 Ws 不可见。
我看到两个可能互补的备选方案来解决这个问题。第一个是创建一个完全自定义的图例并将其传递给 xyplot
的 key
参数(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
中描述的参数详细描述了各种修改选项。
现在,如果您的图例标题很长,并且不想在绘图区域内包含图例,您还可以定义单独的视口,从而让图例在右侧占据更多 space利润。请注意使用 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()
我正在使用 lsmeans 中的 lsmip
来绘制我的模型,
library(lsmeans)
PhWs1 <- lsmip(GausNugget1, Photoperiod:Ws ~ Month,
ylab = "Observed log(number of leaves)", xlab = "Month",
main = "Interaction between Photoperiod and Water stress over the months (3 photoperiods)",
par.settings = list(fontsize = list(text = 15, points = 10)))
但我无法在互联网上获得有关如何处理图例位置、大小、标题等的建议。
我使用 trellis.par.get()
查看参数,但找不到与我的问题相关的参数。从图中可以看出,图例应该是 "Photoperiod*Ws" 但 Ws 不可见。
我看到两个可能互补的备选方案来解决这个问题。第一个是创建一个完全自定义的图例并将其传递给 xyplot
的 key
参数(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
中描述的参数详细描述了各种修改选项。
现在,如果您的图例标题很长,并且不想在绘图区域内包含图例,您还可以定义单独的视口,从而让图例在右侧占据更多 space利润。请注意使用 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()