How to create side by side histograms in base R?

R ProgrammingServer Side ProgrammingProgramming

To create side by side histograms in base R, we first need to create a histogram using hist function by defining a larger limit of X-axis with xlim argument. After that we can create another histogram that has the larger mean and smaller standard deviation so that the bars do not clash with each other and add=T argument must also be added inside the second hist function.

Example

Live Demo

hist(rnorm(5000,mean=5,sd=2.1),col="green",xlim=c(1,20))

Example

hist(rnorm(5000,mean=15,sd=1.25),col="red",add=T)

Output

Published on 07-Dec-2020 06:16:57