How to plot two histograms together in R?

Answer

carrots <- data.frame(length = rnorm(100000,6,2))
cukes <- data.frame(length = rnorm(50000,7,2.5))#Now, combine your two dataframes into one.  First make a new column in each.
carrots$veg <-'carrot'
cukes$veg <-'cuke'#and combine into your new data frame vegLengths
vegLengths <- rbind(carrots, cukes)#now make your lovely plot
ggplot(vegLengths, aes(length, fill = veg))+ geom_density(alpha =0.2)

enter image description here

Now, if you really did want histograms the following will work. Note that you must change position from the default "stack" argument. You might miss that if you don't really have an idea of what your data should look like. A higher alpha looks better there. Also note that I made it density histograms. It's easy to remove the y = ..density.. to get it back to counts.

ggplot(vegLengths, aes(length, fill = veg))+ geom_histogram(alpha =0.5, aes(y = ..density..), position ='identity')

enter image description here

All r Questions

Ask your interview questions on r

Write Your comment or Questions if you want the answers on r from r Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---