无法在组合的boxplot&中调整边距。直方图

时间:2015-03-15 16:33:27

标签: r histogram boxplot

我需要绘制一个组合的boxplot和直方图。虽然我能够将它们一起绘制,但我无法在直方图中打印xlabel abd ylabel。每当我尝试调整边距时,我都会得到错误'数字边距太大'。这是代码段

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之间的值。

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

的边距

由于 内甚enter image description here

2 个答案:

答案 0 :(得分:1)

你可以这样做:

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")

enter image description here

答案 1 :(得分:0)

我认为最好使直方图和箱线图都使用相同的轴(在问题和先前答案中,箱线图中的给定值与直方图中的值不对齐)。

所以我认为最好使用package ggExtra中的ggMarginal

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")

Thanks to Z.Lin for her answer in this question, which helped me first on this topic

enter image description here