将多行文字与晶格图和grid.arrange一起使用

将多行文字与晶格图和grid.arrange一起使用,r,lattice,gridextra,R,Lattice,Gridextra,我正在尝试将A和B标签添加到使用grid.arrange函数组合的两个晶格图中。我无法让B标签显示在我想要的位置。我希望字母A位于顶部图的左侧和上方,字母B位于底部图的左侧和上方。我试着把行号改成不同的数字,但没有成功 以下是一些示例数据: data1<-data.frame(x=c(1:60),y=rep(c("A","B"),each=30),w=rep(c("V1","V2","V3"),times=20)) Plotting Code require(lattice);req

我正在尝试将A和B标签添加到使用grid.arrange函数组合的两个晶格图中。我无法让B标签显示在我想要的位置。我希望字母A位于顶部图的左侧和上方,字母B位于底部图的左侧和上方。我试着把行号改成不同的数字,但没有成功

以下是一些示例数据:

  data1<-data.frame(x=c(1:60),y=rep(c("A","B"),each=30),w=rep(c("V1","V2","V3"),times=20))

Plotting Code
require(lattice);require(grid.Extra)

p1<-bwplot(x~y, data = data1, groups = w, 
       pch = "|", box.width = 1/6,  par.settings =mytheme ,scales=list(x=list(cex=1.2),y=list(cex=1.2)),
       ylab=list("Number of Scallops per Station",fontsize=15),xlab=list("Strata",fontsize=15),
       panel = panel.superpose,
       panel.groups = function(x, y, ..., cex,group.number,col) { 
           panel.bwplot(x + (group.number-1.5)/6,y,...) 
       }) 

p2<-barchart(x~y|w,type="count",layout=c(3,1),data=data1,
    col="light gray", xlab="Binned Number of Scallops per Station",ylab="Count",horizontal=F,
    strip = strip.custom(bg="white",strip.levels = T),as.table=T,origin=0,family="A",
    scales=list(relation="same",alternating=1,x=list(cex=1.2),y=list(cex=1.2)),
    par.settings = list( grid.pars = 
    list(fontfamily = 'serif',cex=1.2)),
    panel=function(x,y,...){
    panel.barchart(x,y,...)})

grid.arrange(p1,p2)

mtext("A",3,line=2,cex=1.2,at=0,family="serif")


 #not in the correct area

mtext("B",3,line=25,cex=1.2,at=0,family="serif")


sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
data1mtext()用于基本图形,网格等价物为grid.text

library(lattice)
library(gridExtra)
library(grid)

p1 <- p2 <- xyplot(1~1)

grid.arrange(p1, p2)
grid.text(c("A","B"), x=0, y=c(0.5, 1), vjust=1, hjust=0, gp=gpar(fontface=4))
库(晶格)
图书馆(gridExtra)
图书馆(网格)

p1我强烈支持使用@baptiste建议的
grid.text
的方法。但是,如果您需要在某些时候使用base-R文本注释和基于网格的图形,请记住在进行其他操作之前,先通过
plot.new()
打开一个新的绘图设备。这里有一种基于
text()
的快速方法可以解决这个问题,但请记住,只要有可能,就要使用网格功能

library(lattice)
library(gridExtra)
library(gridBase)

## create and combine plots
p1 <- p2 <- xyplot(1~1)
p <- grid.arrange(p1, p2)

## open new plot device
plot.new()

## navigate to base graphics plot region and insert grob
vps <- gridBase::baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)

grid.draw(p)

## add text annotations 
text(x = 0, y = c(.975, .45), labels = c("A", "B"), font = 2)
库(晶格)
图书馆(gridExtra)
图书馆(gridBase)
##创建并合并绘图
p1