Following is the summary of commands used to create histogram using ggplot:
In the code examples below, diamonds dataset from ggplot2 package is used. To work with examples below, load the ggplot2 library prior to executing the commands given below.
Following are some of the common attributes that could be used with geom_histogram method while creating a histogram using ggplot2 library:
Following is the most simplistic command which could be used to create a Histogram.
# Pay attention to usage of ggplot method with additional layer of geom_histogram() method
ggplot(diamonds, aes(x=price)) + geom_histogram()
Following could be used to create Histogram with labels and additional attributes passed to geom_histogram method such as col, fill, alpha etc.
# Pay attention to usage of attributes such as col, fill, alpha. Also, note labs method
# for assigning labels and title
ggplot(diamonds, aes(x=price)) + geom_histogram(col="red", fill="green", alpha=0.4) +
labs (x="Price", y="Count", title="Histogram for Price")
Following commands could be used to assign varying color shades to bins (blue by default).
# Paint different bins with different shares of blue (default color) : Parameter aes(fill=..count..)
ggplot(diamonds, aes(x=price)) + geom_histogram(col="red", aes(fill=..count..)) +
labs (x="Price", y="Count", title="Histogram for Price")
Following commands could be used assign different color shades varying from low to highest bin heights
# Paint different bins with different shades of custom colors using scale_fill_gradient method
ggplot(diamonds, aes(x=price)) + geom_histogram(col="red", aes(fill=..count..) ) +
labs (x="Price", y="Count", title="Histogram for Price") +
scale_fill_gradient("Count", low="yellow", high="red")
Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…
Last updated: 2nd May, 2024 The success of machine learning models often depends on the…
When working on a machine learning project, one of the key challenges faced by data…
Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…
Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…
Last updated: 1st May, 2024 In this post, you will learn the concepts related to…