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
I am fairly new to R and statistical computing languages in general so I would appreciate it if you would answer my frustrating problem.
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.
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.
The example comes from using the plotrix
package. 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With