从格子图中删除内边距

时间:2015-09-01 13:11:34

标签: r ggplot2 lattice

感谢"Combine a ggplot2 object with a lattice object in one plot"中的优秀答案以及一些进一步的想法,我可以在lattice旁边绘制ggplot地块:

library(ggplot2)
library(lattice)
library(gtools)
library(plyr)
library(grid)
library(gridExtra)

set.seed(1)
mdat <- data.frame(x = rnorm(100), y = rnorm(100), veryLongName = rnorm(100),
                   cluster = factor(sample(5, 100, TRUE))) 
cols <- c("x", "y", "veryLongName")
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) + theme(legend.position = "top")
sc3d <- cloud(veryLongName ~ x + y, data = mdat, groups = cluster)

scG  <- ggplotGrob(sc)
sc3dG <- gridExtra:::latticeGrob(sc3d)
ids <- which(scG$layout$name %in%
            c(paste("axis", c("l", "b"), sep = "-"), "panel"))
scG$grobs[ids[c(2, 4, 7)]] <- list(nullGrob(), nullGrob(), nullGrob())
grid.newpage()
grid.draw(scG)
pushViewport(viewport(0, 0, width = .515, height = .46,
                      just = c("left", "bottom")))
grid.rect()
grid.draw(sc3dG)

正如你在图片中看到的那样,在格子图周围有一些边缘,并且在它的顶部,z轴的轴标签被切割(情况不是我绘制lattice图单独的)。

那么我怎样才能摆脱这种行为,从而如何解决以下两个问题:

  1. 摆脱viewportlattice情节
  2. 之间的内部边距
  3. 避免lattice图中的标签被剪切。
  4. 我尝试使用clip的{​​{1}}选项,但没有成功。那么,该怎么办?

    enter image description here enter image description here

1 个答案:

答案 0 :(得分:3)

这些设置可能位于?xyplot的某个位置,但我发现它读取互联网的速度更快,

enter image description here

theme.novpadding <-
  list(layout.heights =
         list(top.padding = 0,
              main.key.padding = 0,
              key.axis.padding = 0,
              axis.xlab.padding = 0,
              xlab.key.padding = 0,
              key.sub.padding = 0,
              bottom.padding = 0),
       axis.line = list(col = 0),
       clip =list(panel="off"),
       layout.widths =
         list(left.padding = 0,
              key.ylab.padding = 0,
              ylab.axis.padding = 0,
              axis.key.padding = 0,
              right.padding = 0))


sc3d <- cloud(veryLongName ~ x + y, data = mdat, groups = cluster,
              par.settings = theme.novpadding )

scG  <- ggplotGrob(sc)
sc3dG <- grobTree(gridExtra:::latticeGrob(sc3d), 
                  rectGrob(gp=gpar(fill=NA,lwd=1.2)))
ids <- which(scG$layout$name %in%
               c(paste("axis", c("l", "b"), sep = "-"), "panel"))
scG$grobs[ids[c(2, 4, 7)]] <- list(nullGrob(), sc3dG, nullGrob())
grid.newpage()
grid.draw(scG)