Code Sample – Draw Histogram and Density Plot
Histrogram and density plot are very useful for examining the spread of a data variable. Following R commands with ggplot package helps in drawing histogram and density plots. As I am explaining with ggplot package, I am using diamonds data which comes with ggplot package. Pay attention to some of the following:
- Draw Histogram: Command “ggplot(data) + geom_histogram(aes(x=variableName))” is used to draw the histogram. One could also provide binwidth as an additional parameter to geom_histogram function
- Draw Density Plot: Command “ggplot(data) + geom_density(aes(x=variableName))” is used to create the density plot.
# Histogram to evaluate the spread of carat data ggplot(diamonds) + geom_histogram(aes(x=carat)) # Density plot to evaluate the spread of carat data ggplot(data=diamonds) + geom_density(aes(x=carat))
He has also authored the book, Building Web Apps with Spring 5 and Angular.
Latest posts by Ajitesh Kumar (see all)
- What, When & Why of Regularization in Machine Learning? - June 2, 2019
- Unit Tests & Data Coverage for Machine Learning Models - May 11, 2019
- ML Models Confusion Matrix Explained with Examples - March 30, 2019