Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Side by Side histograms in the Same Graph in R?

Tags:

This should actually be really simple but I'm having a really hard time finding a solution to this problem.

I have two very simple numeric vectors in R. I am simply trying to plot a histogram with them. However I would like them to be on the same graph. The tricky part is R overlaps these two histograms by default. I would like the bins to be simply side by side so I can get a better visual representation of the data.

Basically this is what I want to do

enter image description here

I am fairly new to R and statistical computing languages in general so I would appreciate it if you would answer my frustrating problem.

like image 879
user2331197 Avatar asked Apr 29 '13 07:04

user2331197


People also ask

How do I plot multiple histograms on the same graph in R?

To create multiple histograms in ggplot2, we use ggplot() function and geom_histogram() function of the ggplot2 package. To visualize multiple groups separately we use the fill property of aesthetics function to color the plot by a categorical variable.

How do you plot two variables on a histogram in R?

In this method, to create a histogram of two variables, the user has to first install and import the ggplot2 package, and then call the geom_histrogram with the specified parameters as per the requirements and needs to create the dataframe with the variable to which we need the histogram in the R programming language.


1 Answers

The example comes from using the plotrixpackage. Code was found here. You will first need to install that package before you can access the multihist function:

#install.packages("plotrix")
require(plotrix)

l <- list(rnorm(50),rnorm(50,sd=2),rnorm(50,mean=3))
multhist(l)

enter image description here

like image 70
Marc in the box Avatar answered Dec 20 '22 07:12

Marc in the box