将ggplot2对象与格子对象组合在一个绘图中 [英] Combine a ggplot2 object with a lattice object in one plot

查看:518
本文介绍了将ggplot2对象与格子对象组合在一个绘图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个 ggplot2 与一个点阵绘图对象结合。由于这两个包基于 grid 我想知道这是否可能?理想情况下,我会尽一切努力在 ggplot2 中,但是我无法绘制出3d散点图。 所以假设我有以下数据:

  set.seed(1)
mdat< - data.frame(x = rnorm(100) ,y = rnorm(100),z = rnorm(100),
cluster = factor(sample(5,100,TRUE)))

首先,我想在 ggplot2 中创建散点图矩阵:

< ($ p

$ col $< - c(x,) (组合(3,2,cols),1,函数(r)
data.frame(cluster = mdat $ cluster,
) var.x = r [1],
x = mdat [[r [1]]],
var.y = r [2],
y = mdat [[r [2]] ]))

sc <-ggplot(allS,aes(x = x,y = y,color = cluster))+ geom_point()+
facet_grid(var.x〜var .y)

到目前为止这么好。现在我想创建一个包含所有变量的 lattice散点图:

  library(lattice)
sc3d < - cloud(z〜x + y,data = mdat,groups = cluster)

现在我想在一个图中结合 sc sc3d 。我怎样才能做到这一点?也许在 grid gridExtra pushViewport )的帮助下, arrangeGrob ?)?或者我可以在 ggplot 中产生一个3d散点图?理想情况下,我希望在 ggplot 的空面板中看到3d图,但我想这个问题甚至太多了,所以对于初学者,我会很乐意学习如何我们可以并排安排这两幅图。




解决方案

  library(gridExtra);库(晶格);库(ggplot2)
grid.arrange(xyplot(1〜1),qplot(1,1))



你可以用gtable中的格子替换空的面板,但它不会'由于坐标轴等原因看起来非常好。

  g < -  ggplotGrob(sc)
lg < - gridExtra ::: latticeGrob(sc3d)
ids< - which(g $ layout $ name ==panel)
remove< - ids [2]
g $ grobs [[remove ]] <-g
grid.newpage()
grid.draw(g)


I would like to combine a ggplot2 with a lattice plot object. Since both packages are based on grid I was wondering whether this is possible? Ideally, I would do everything in ggplot2 but I cannot plot a 3d scatter.

So assume I have the following data:

set.seed(1)
mdat <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100),
                   cluster = factor(sample(5, 100, TRUE)))

First, I want to create a scatterplot matrix in ggplot2:

library(ggplot2)
library(gtools)
library(plyr)

cols <- c("x", "y", "z")
allS <- adply(combinations(3, 2, cols), 1, function(r)
    data.frame(cluster = mdat$cluster,
          var.x = r[1],
          x = mdat[[r[1]]],
          var.y = r[2],
          y = mdat[[r[2]]]))

sc <- ggplot(allS, aes(x = x, y = y, color = cluster)) + geom_point() +
      facet_grid(var.x ~ var.y)

So far so good. Now I want to create a lattice 3d scatterplot with all the variables together:

library(lattice)
sc3d <- cloud(z ~ x + y, data = mdat, groups = cluster)

Now I would like to combine sc and sc3d in one single plot. How can I achieve that? Maybe with the help of grid or gridExtra (pushViewport, arrangeGrob?)? Or can I produce a 3d scatterplot in ggplot? Ideally, I would like to see the 3d plot in the empty panel pf the ggplot but I guess that's asked even too much, so for starters I would be very happy to learn how we could arrange these two plots side by side.

解决方案

library(gridExtra); library(lattice); library(ggplot2)
grid.arrange(xyplot(1~1), qplot(1,1))

You can replace the empty panel by the lattice grob within the gtable, but it doesn't look very good due to the axes etc.

g <- ggplotGrob(sc)
lg <- gridExtra:::latticeGrob(sc3d)
ids <- which(g$layout$name == "panel")
remove <- ids[2]
g$grobs[[remove]] <- lg
grid.newpage()
grid.draw(g)

这篇关于将ggplot2对象与格子对象组合在一个绘图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆