Create histogram with horizontal boxplot on top in base R.


To create a histogram with horizontal boxplot on top in base R, we first need to define the layout of the plotting area with layout function and par function margin (mar) then the boxplot will be created and after that the histogram will be created. While creating the boxplot and histogram we need to make sure that the ylim for boxplot and xlim for histogram are same.

Check out the below Example to understand how it can be done.

Example

To create a histogram with horizontal boxplot on top in base R, use the following snippet −

x<-rnorm(100)
layout(mat=matrix(c(1,2),2,1,byrow=TRUE),height=c(2,4))
par(mar=c(4,3,1,2))
boxplot(x,horizontal=TRUE,outline=FALSE,ylim=c(-4,4))

Output

If you execute the above given snippet, it generates the following Output −

To create a histogram with horizontal boxplot on top in base R add the following code to the above snippet −

x<-rnorm(100)
layout(mat=matrix(c(1,2),2,1,byrow=TRUE),height=c(2,4))
par(mar=c(4,3,1,2))
hist(x,xlim=c(-4,4))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

Updated on: 05-Nov-2021

557 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements