Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Histogram with marginal boxplot in R

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)
like image 717
Andy Avatar asked Apr 18 '13 12:04

Andy


1 Answers

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

like image 193
Didzis Elferts Avatar answered Sep 20 '22 17:09

Didzis Elferts