Histogram
Draw a basic histogram
# Load ggplot2 package
library(ggplot2)
# Load the diamonds dataset from the ggplot2 package
data(diamonds)
# Draw the basic histogram
ggplot(diamonds, aes(x = price)) +
geom_histogram()

Change the bin size
ggplot(diamonds, aes(x = price)) +
geom_histogram(bins = 50)
Here, bin size is changed to 50.

Use fill
and col
as attribute:
fill
and col
as attribute:Here, we will use fill
and col as an attribute with the geom_histogram()
function.
Change the color of the histogram
ggplot(diamonds, aes(x = price)) +
geom_histogram(bins = 50, fill = "turquoise4")
Output:

Add color to the boundary of the histogram
ggplot(diamonds, aes(x = price)) +
geom_histogram(bins = 50, fill = "turquoise4", col = "deeppink2")
Output:

Use fill
as aesthetic:
fill
as aesthetic:Instead of using the fill
with geom_histogram()
function if we use them inside aes()
, then we will get different visualization.
# Use fill as aesthetic
ggplot(diamonds, aes(x = price, fill=cut)) +
geom_histogram()
Here, fill=cut
, will color the histogram based on cut type in the diamonds dataset.
Last updated