R 如何使用ggplot2减少从箱线图到边的空间?

R 如何使用ggplot2减少从箱线图到边的空间?,r,ggplot2,boxplot,r-grid,R,Ggplot2,Boxplot,R Grid,我想在直方图下方放置一个箱线图。我已经知道了怎么做,但是boxplot和直方图大小相同,我想缩小boxplot。 当我减小宽度时,到边的空间保持不变。然而,我想减少整个东西的宽度 以下是我目前掌握的情况: library(ggplot2) h = ggplot(mtcars, aes(x=hp)) + geom_histogram(aes(y = ..density..)) + scale_x_continuous(breaks=c(100, 200, 300), limits=c(0,

我想在直方图下方放置一个
箱线图。我已经知道了怎么做,但是
boxplot
和直方图大小相同,我想缩小
boxplot
。 当我减小宽度时,到边的空间保持不变。然而,我想减少整个东西的宽度

以下是我目前掌握的情况:

library(ggplot2)
h = ggplot(mtcars, aes(x=hp)) +
  geom_histogram(aes(y = ..density..)) +
  scale_x_continuous(breaks=c(100, 200, 300), limits=c(0,400)) +
  geom_density(, linetype="dotted")

b <- ggplot(mtcars,aes(x=factor(0),hp))+geom_boxplot(width=0.1) +
  coord_flip(ylim=c(0,400)) +
  scale_y_continuous(breaks=c(100, 200, 300)) +
  theme(axis.title.y=element_blank(),
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank())

library(gridExtra)
plots <- list(h, b)
grobs <- list()
widths <- list()
for (i in 1:length(plots)){
  grobs[[i]] <- ggplotGrob(plots[[i]])
  widths[[i]] <- grobs[[i]]$widths[2:5]
}
maxwidth <- do.call(grid::unit.pmax, widths)
for (i in 1:length(grobs)){
  grobs[[i]]$widths[2:5] <- as.list(maxwidth)
}
do.call("grid.arrange", c(grobs, ncol=1))

比例和我想要的完全一样,但我不知道如何调整上面的第一个示例,以便再次对齐轴


有人吗?

你只需要在
网格中使用你的宽度修正grobs,而不是原始绘图。排列
调用

grid.arrange(heights = c(4, 1), grobs[[1]], grobs[[2]])

看一看:谢谢,但那不像我想要的。如果您指的是grid.arrange的高度选项,请告诉我如何相应地调整我的示例。到目前为止,我所有的尝试都失败了。对于这样一个重复性很好的问题,我几乎没有尝试过。
grid.arrange(heights = c(4, 1), grobs[[1]], grobs[[2]])