> For the complete documentation index, see [llms.txt](https://ar-riyaz.gitbook.io/r-for-bioinformatics/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ar-riyaz.gitbook.io/r-for-bioinformatics/data-visualization-in-r/ggplot2-package/histogram.md).

# Histogram

## Draw a basic histogram

```r
# 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()
```

<figure><img src="https://3681152927-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvUtrdiIkCrBX60yTgn1m%2Fuploads%2FROCoYaSqN6k0dsomOfKr%2Fimage.png?alt=media&amp;token=42505573-e521-4872-80a8-3d0cc30b63b1" alt=""><figcaption><p>A basic histogram in ggplot2</p></figcaption></figure>

### Change the bin size

```
ggplot(diamonds, aes(x = price)) + 
    geom_histogram(bins = 50)
```

Here, bin size is changed to <mark style="color:purple;">**50**</mark>.

<figure><img src="https://3681152927-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvUtrdiIkCrBX60yTgn1m%2Fuploads%2Fcq5QIu5c9QfQPEDXp5GQ%2Fimage.png?alt=media&amp;token=a6f0127a-514a-4f32-ba83-e346bdf29a22" alt=""><figcaption><p>A histogram with bin size 50</p></figcaption></figure>

## Use <mark style="color:red;">`fill`</mark> and <mark style="color:red;">`col`</mark> as attribute:

Here, we will use <mark style="color:red;">`fill`</mark> and <mark style="color:red;">col</mark> as an attribute with the <mark style="color:red;">`geom_histogram()`</mark> function.&#x20;

### Change the color of the histogram

```r
ggplot(diamonds, aes(x = price)) +
    geom_histogram(bins = 50, fill = "turquoise4")
```

**Output:**

<figure><img src="https://3681152927-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvUtrdiIkCrBX60yTgn1m%2Fuploads%2FlVk5eop15TzzcCuOWpAA%2Fimage.png?alt=media&amp;token=93ca71b2-bd1e-442f-b17c-00fdbe8355a3" alt=""><figcaption><p>Histogram with color</p></figcaption></figure>

### Add color to the boundary of the histogram

```r
ggplot(diamonds, aes(x = price)) +
    geom_histogram(bins = 50, fill = "turquoise4", col = "deeppink2")
```

**Output:**

<figure><img src="https://3681152927-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvUtrdiIkCrBX60yTgn1m%2Fuploads%2FaMD6idz3CAbl4y4H85cp%2Fimage.png?alt=media&amp;token=5a8f947e-89aa-42f3-b11f-e1c17337a431" alt=""><figcaption></figcaption></figure>

## Use <mark style="color:red;">`fill`</mark>  as aesthetic:

Instead of using the <mark style="color:red;">`fill`</mark> with <mark style="color:red;">`geom_histogram()`</mark> function if we use them inside <mark style="color:red;">`aes()`</mark>, then we will get different visualization.

```r
# Use fill as aesthetic
ggplot(diamonds, aes(x = price, fill=cut)) +
    geom_histogram()
```

Here, <mark style="color:red;">`fill=cut`</mark>, will color the histogram based on <mark style="color:red;">cut</mark> type in the diamonds dataset.
