使用grid.arrange(gridExtra)和lattice绘图的全局图例

3

我正在使用 xyplot (lattice) 生成四个图形,并将它们与 grid.arrange (gridExtra) 结合起来。

我希望获得一个有公共全局图例的图形。我所达到的最接近的结果如下。它们必须是在矩阵布局中,否则一个选项是将它们放在一列中,并只包括顶部或底部的图例。

# Load packages
require(lattice)
require(gridExtra)

# Generate some values
x1<-rnorm(100,10,4)
x2<-rnorm(100,10,4)
x3<-rnorm(100,10,4)
x4<-rnorm(100,10,4)
y<-rnorm(100,10,1)
cond<-rbinom(100,1,0.5)
groups<-sample(c(0:10),100,replace=TRUE)
dataa<-data.frame(y,x1,x2,x3,x4,cond,groups)

# ploting function
plott<-function(x){ 
xyplot(y~x|cond,groups=groups,
       col = gray(seq(0.01,0.7,length=length(levels(as.factor(groups))))),
       pch = 1:length(levels(as.factor(groups))),
       key = list(space="top",
                  text = list(as.character(levels(as.factor(groups)))),
                  points = TRUE, lines = TRUE, columns = 3,
                  pch = 1:length(levels(as.factor(groups))), 
                  col = gray(seq(0.01,0.7,length=length(levels(as.factor(groups))))),
                  cex=1))
}
plot1<-plott(x=x1)
plot2<-plott(x=x2)
plot3<-plott(x=x3)
plot4<-plott(x=x4)


grid.arrange(plot1,plot2,plot2,plot4,ncol=2)

在类似的帖子中,我看到可以使用ggplot2来执行,例如这里这里,但是是否有一种方法可以使用gridExtra和基于lattice的绘图(例如xyplot)来包含全局公共图例?
谢谢。

1
是的,相同的原则应该适用于格子图。毕竟,lattice和ggplot2都使用网格图形。我无法向您展示具体细节,因为我对lattice不太熟悉。 - Roland
@Roland,你能解释一下ggplot2和lattice之间的区别吗?它们似乎经常一起使用 - 但是你能避免使用lattice,只使用ggplot2或者反过来,只使用lattice和基本的grid命令吗?ggplot2提供了哪些lattice没有的功能?我不确定该选择哪种方法来回答这个简单的图例问题在这里 - hhh
ggplot2和lattice都建立在grid包的基础之上,并为其提供了高级接口。它们并不经常一起使用(实际上几乎从来没有),因为两者都提供了相同的功能-个人而言,我不使用lattice。 - Roland
3个回答

2

我设法制作出更接近我最初想象的东西。为此,我包含了一个额外的图形元素,并在grid.arrange中使用了layout_matrix选项来最小化其影响。这样我就保留了图例并几乎排除了绘图。

# Load packages
require(lattice)
require(gridExtra)

# Generate some values
x1<-rnorm(100,10,4)
x2<-rnorm(100,10,4)
x3<-rnorm(100,10,4)
x4<-rnorm(100,10,4)
y<-rnorm(100,10,1)
cond<-rbinom(100,1,0.5)
groups<-sample(c(0:10),100,replace=TRUE)
dataa<-data.frame(y,x1,x2,x3,x4,cond,groups)

# ploting function
plottNolegend<-function(x){ 
  xyplot(y~x|cond,groups=groups,
         col = gray(seq(0.01,0.7,length=length(levels(as.factor(groups))))),
         pch = 1:length(levels(as.factor(groups)))
  )
}
plott<-function(x){ 
  xyplot(y~x|cond,groups=groups,
         col = gray(seq(0.01,0.7,length=length(levels(as.factor(groups))))),
         pch = 1:length(levels(as.factor(groups))),
         key = list(space="top",
                    text = list(as.character(levels(as.factor(groups)))),
                    points = TRUE, lines = TRUE, columns = 3,
                    pch = 1:length(levels(as.factor(groups))), 
                    col = gray(seq(0.01,0.7,length=length(levels(as.factor(groups))))),
                    cex=1))
}
plot1<-plottNolegend(x=x1)
plot2<-plottNolegend(x=x2)
plot3<-plottNolegend(x=x3)
plot4<-plottNolegend(x=x4)
legend<-plott(x=x4)



lay <- rbind(c(1,2),
             c(1,2),
             c(3,4),
             c(3,4),
             c(5,5))


grid.arrange(plot1,plot2,plot2,plot4,legend, layout_matrix = lay)

带有全局图例的绘图,位于底部 更新:答案比我预期的要简单得多。感谢大家的帮助。

    # Load packages
require(lattice)
require(gridExtra)
require(grid)

# Generate some values
x1<-rnorm(100,10,4)
x2<-rnorm(100,10,4)
x3<-rnorm(100,10,4)
x4<-rnorm(100,10,4)
y<-rnorm(100,10,1)
cond<-rbinom(100,1,0.5)
groups<-sample(c(0:10),100,replace=TRUE)
dataa<-data.frame(y,x1,x2,x3,x4,cond,groups)

# ploting function
plott<-function(x){ 
  xyplot(y~x|cond,groups=groups,
         col = gray(seq(0.01,0.7,length=length(levels(as.factor(groups))))),
         pch = 1:length(levels(as.factor(groups))),
         key = NULL)
}
plot1<-plott(x=x1)
plot2<-plott(x=x2)
plot3<-plott(x=x3)
plot4<-plott(x=x4)


grid.arrange(plot1,plot2,plot2,plot4,ncol=2)

KeyA<-list(space="top",
     text = list(as.character(levels(as.factor(groups)))),
     points = TRUE, lines = TRUE, columns = 11,
     pch = 1:length(levels(as.factor(groups))), 
     col = gray(seq(0.01,0.7,length=length(levels(as.factor(groups))))),
     cex=1)



draw.key(KeyA, draw = TRUE, vp =
           viewport(.50, .99))

你能指导如何使用Lattice包在这里构建图例吗?链接为:http://stackoverflow.com/questions/40699787/r-shared-legend-from-a-subplot-to-the-2x2-grid-arrange-panel-with-lattice - hhh

2

一个可能的解决方案是使用ggplot,这里有提示

这是生成的图形:

my.cols <- 1:3

my.grid.layout <- rbind(c(1,2),
                        c(3,3))

g_legend<-function(a.gplot){
    tmp <- ggplot_gtable(ggplot_build(a.gplot))
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
    legend <- tmp$grobs[[leg]]
    return(legend)
  }

legend.plot <- ggplot(iris, aes(x=Petal.Length, y=Sepal.Width,colour=Species)) +
                                  geom_line(size=1) + # legend should show lines, not points or rects ...
                                  theme(legend.position="right", legend.background = element_rect(colour = "black"),
                                        legend.key = element_rect(fill = "white")) + # position, box and background colour of legend
                                  scale_color_manual(values=my.cols, name = "Categories") + # manually insert colours as used in corresponding xyplot
                                  guides(colour = guide_legend(reverse=T)) # inverts order of colours in legend

mylegend <- g_legend(legend.plot)
plot1 <- xyplot(Sepal.Width ~ Petal.Length, groups = Species, data = iris, type = 'l',
                par.settings = simpleTheme(col=my.cols))
plot2 <- xyplot(Sepal.Length ~ Petal.Length, groups = Species, data = iris, type = 'l',
                par.settings = simpleTheme(col=my.cols))


grid.arrange(plot1,plot2,mylegend,layout_matrix=my.grid.layout,             
         top=textGrob(gp=gpar(col='black',fontsize=20),"Some useless example"))

1

我认为更好的解决方案是使用 latticeExtra 中的 c.trellis

library(latticeExtra)
c(plot1, plot2, plot3, plot4)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接