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()
A basic histogram in ggplot2

Change the bin size

Here, bin size is changed to 50.

A histogram with bin size 50

Use 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

Output:

Histogram with color

Add color to the boundary of the histogram

Output:

Use fill as aesthetic:

Instead of using the fill with geom_histogram() function if we use them inside aes(), then we will get different visualization.

Here, fill=cut, will color the histogram based on cut type in the diamonds dataset.

Last updated