无法调整组合箱线图和直方图中的边距

Unable to adjust margins in combined boxplot & histogram

我需要绘制组合箱线图和直方图。虽然我能够将它们绘制在一起,但我无法在直方图中打印 xlabel abd ylabel。每次我尝试调整边距时,我都会收到错误 'figure margins too large'。这是代码片段

value <- rnorm(300,mean=100,sd=20)
nf <- layout(mat = matrix(c(1,2),2,1, byrow=TRUE),  height = c(1,3)) 
par(mar=c(2,2,1,1))
boxplot(value, horizontal=TRUE,  outline=TRUE,ylim=c(0,160), 
      frame=F, col = "green1")
hist(value,breaks=40,xlab="Runs",ylab="Runs frequency", main = "Score")

值取 0 到 250 之间的值。

我包括下面的情节。请告诉我如何为此调整边距

谢谢 象头神

你可以这样做:

value <- rnorm(300,mean=100,sd=20)
nf <- layout(mat = matrix(c(1,2),2,1, byrow=TRUE),  height = c(1,3)) 
par(mar=c(4,4,1,1))
boxplot(value, horizontal=TRUE,  outline=TRUE,ylim=c(0,160), 
        frame=F, col = "green1")
hist(value,breaks=40,xlab="Runs",ylab="Runs frequency", main = "Score")

我认为让直方图和箱线图使用同一轴要好得多(在问题和之前的答案中,箱线图中的给定值与直方图中的相同值不对齐)。

所以我认为使用 ggMarginal 来自 package ggExtra

会更好
library(ggplot2)
library(ggExtra) 

value <- rnorm(300,mean=100,sd=20) # your value
df <- data.frame(value) # I store it in a data.frame to be used with ggplot

p <- ggplot(df,
       aes(x = value)) +
  geom_point(    # ggMarginal works with scatterplots, so we create it
    aes(y = 20), # this value is arbitrary, just to be inside the plotting area
    alpha = 0) + # not to be printed
  geom_histogram()

ggMarginal(p, type = "boxplot", margins = "x")