Histogram with marginal boxplot in R

10,148

One solution would be to set ylim= in boxplot() to the same range as xlim= in hist().

set.seed(123)
data <- rnorm(1000)
nf <- layout(mat = matrix(c(1,2),2,1, byrow=TRUE),  height = c(1,3))
par(mar=c(5.1, 4.1, 1.1, 2.1))
boxplot(data, horizontal=TRUE,  outline=FALSE,ylim=c(-4,4))
hist(data,xlim=c(-4,4))

enter image description here

Share:
10,148
Andy
Author by

Andy

Updated on July 16, 2022

Comments

  • Andy
    Andy almost 2 years

    How make matching X-axis in histogram with marginal boxplot?

    data <- rnorm(1000)
    nf <- layout(mat = matrix(c(1,2),2,1, byrow=TRUE),  height = c(1,3))
    layout.show(nf)
    par(mar=c(5.1, 4.1, 1.1, 2.1))
    boxplot(data, horizontal=TRUE,  outline=FALSE)
    hist(data)