2

我希望绘制一些紧密间隔的图形,如下面的玩具示例所示:

library(ggplot2)
library(gridExtra)
set.seed(314159)
n <- 100
data <- data.frame(x = rnorm(n), y = rnorm(n), z = rep("dummy var", n))

p00 <- ggplot(data, aes(x)) + stat_density() + theme(plot.margin = unit(c(0,0,0,0), units = "lines" ), axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank()) + labs(x = NULL, y = NULL)
p01 <- ggplot(data, aes(x, y)) + geom_point() + theme(plot.margin = unit(c(0,0,0,0), units = "lines" ), axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank()) + labs(x = NULL, y = NULL)
p10 <- ggplot(data, aes(y, x)) + geom_point() + theme(plot.margin = unit(c(0,0,0,0), units = "lines" ), axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank()) + labs(x = NULL, y = NULL)
p11 <- ggplot(data, aes(y)) + stat_density() + theme(plot.margin = unit(c(0,0,0,0), units = "lines" ), axis.text = element_blank(), axis.title = element_blank(), axis.ticks = element_blank()) + labs(x = NULL, y = NULL)

grid.arrange(p00, p01, p10, p11, ncol = 2)

尽管我尽了最大的努力,但在从图表中删除刻面条后尝试这样做时,我仍然无法克服出现的并发症。在以下示例中,我通过对虚拟变量进行刻面,为每个图形添加了水平和垂直条带:

p00 <- p00 + facet_grid(z ~ z)
p01 <- p01 + facet_grid(z ~ z)
p10 <- p10 + facet_grid(z ~ z)
p11 <- p11 + facet_grid(z ~ z)

grid.arrange(p00, p01, p10, p11, ncol = 2)

接下来,我根据本文中概述的程序移除条。但是,相比之下,结果图的间距相当大:

p00 <- p00 + theme(plot.margin = unit(c(0,0.5,0.5,0), units = "lines" ), strip.background = element_blank(), strip.text = element_blank())
p01 <- p01 + theme(plot.margin = unit(c(0,0.5,0.5,0), units = "lines" ), strip.background = element_blank(), strip.text = element_blank())
p10 <- p10 + theme(plot.margin = unit(c(0,0.5,0.5,0), units = "lines" ), strip.background = element_blank(), strip.text = element_blank())
p11 <- p11 + theme(plot.margin = unit(c(0,0.5,0.5,0), units = "lines" ), strip.background = element_blank(), strip.text = element_blank())

grid.arrange(p00, p01, p10, p11, ncol = 2)

任何关于如何减少图表间距的建议将不胜感激。

4

1 回答 1

1

要删除与轴关联的所有元素,除了您设置为 的元素外,还需要element_blank将刻度边距和刻度长度设置为零。但空间将留给刻面条。将背景和文本设置为element_blank不会影响条带的高度和宽度。为了移除这些条带,我使用了操纵 gtable 布局的函数。但是,我认为最好在地块之间留一些空白。我将一个小的绘图边距设置为 0.2 行。

library(ggplot2)
library(gridExtra)
set.seed(314159)
n <- 100
data <- data.frame(x = rnorm(n), y = rnorm(n), z1 = rep("dummy var", n), z2 = rep("dummy var", n))

theme = theme(plot.margin = unit(c(.2,.2,.2,.2), units = "lines"), 
         axis.text = element_blank(), 
         axis.title = element_blank(), 
         axis.ticks = element_blank(), 
         axis.ticks.length = unit(0, "lines"))
labs = labs(x = NULL, y = NULL) 

p00 <- ggplot(data, aes(x)) + stat_density() + theme + labs + facet_grid(z1 ~ z2)
p01 <- ggplot(data, aes(x, y)) + geom_point() + theme + labs + facet_grid(z1 ~ z2)
p10 <- ggplot(data, aes(y, x)) + geom_point() + theme + labs + facet_grid(z1 ~ z2)
p11 <- ggplot(data, aes(y)) + stat_density() + theme + labs + facet_grid(z1 ~ z2)

这是操纵 gtable 布局的地方。

# Get the four gtables (and the four plots) into a list
pList = list(p00, p01, p10, p11)
gList = lapply(pList, ggplotGrob)

# Remove the top strip from each plot
stripT <- subset(gList[[1]]$layout, grepl("strip-t", gList[[1]]$layout$name))
gList = lapply(gList, function(x) x[-stripT$t, ])

# Remove the right strip from each plot
stripR <- subset(gList[[1]]$layout, grepl("strip-r", gList[[1]]$layout$name))
gList = lapply(gList, function(x) x[, -stripR$r])

# Draw the revised plots
nCol <- floor(sqrt(length(pList)))
do.call(grid.arrange, c(gList, ncol = nCol))

在此处输入图像描述

编辑:使用修改后的数据和绘图。

library(grid)

data <- data.frame(x = rnorm(n), y = rnorm(n), z = rep("dummy var", n), u = seq(1, n) %% 2) 
p01 <- ggplot(data, aes(x, y)) + geom_point() + theme + labs + facet_grid(z ~ u)

g = ggplotGrob(p01)
stripT = subset(g$layout, grepl("strip-t", g$layout$name))
g = g[-stripT$t, ]
stripR = subset(g$layout, grepl("strip-r", g$layout$name))
g = g[, -stripR$r]

grid.draw(g) # Still got the space between the facets

g$widths  # where is the space? it's the 5.55 pt width

g$widths[[5]] = unit(0, "lines") # remove it completely
g$width
grid.draw(g)
于 2015-01-30T05:59:47.787 回答