合并ggplots,但固定图的大小/比率

MER*_*ose 2 r ggplot2

我结合了两个情节。arrangeGrob()压缩它们,使新图像的大小与单独的图像相同。如何在保留比例/尺寸的同时排列它们?

require(ggplot2)
require(gridExtra)

dat <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")

frqncy <- as.data.table(table(dat$child))#
frqncy$V1 <- as.numeric(frqncy$V1)

plot1 <- ggplot(frqncy, aes(x=V1, y= N)) +
    geom_histogram(stat="identity", binwidth = 2.5)
plot2 <- ggplot(frqncy, aes(x=V1, y= N)) +
    geom_density(stat="identity")

plot <- arrangeGrob(plot1, plot2)
Run Code Online (Sandbox Code Playgroud)

Plot 看起来像

合并后图变小

我没有发现任何参数ggplot()arrangeGrob()固定输入比率的参数。

编辑:额外的复杂性源于中的轴标签的定义arrangeGrob(),即

plot <- arrangeGrob(plot1, plot2, left="LHS label")
Run Code Online (Sandbox Code Playgroud)

然后,新的文件不会automaticall收缩到的最小高度/宽度组合plot1plot2

bap*_*ste 7

还有其他几种选择,具体取决于您想要的*

library(ggplot2)
p = qplot(1, 1)
grid.arrange(p, p, respect=TRUE) # both viewports are square
grid.arrange(p, p, respect=TRUE, heights=c(1,2)) # relative heights

p1 = p + theme(aspect.ratio=3)
grid.arrange(p,p1, respect=TRUE) # one is square, the other thinner
Run Code Online (Sandbox Code Playgroud)

*:长宽比通常不是绘图的明确定义的属性(除非手动设置),因为默认是将绘图扩展到绘图窗口/设备/视口定义的可用空间。

  • `respect = TRUE`,这就是我所需要的。我想知道为什么它不在纪录片中。 (2认同)