# R ggplot2 Histogram

The R ggplot2 Histogram is very useful to visualize the statistical information that can organize in specified bins (breaks, or range). Though, it looks like a Barplot, R ggplot Histogram display data in equal intervals.

Let us see how to Create a ggplot Histogram, Format its color, change its labels, alter the axis. Next, adding the density curves and plot multiple Histograms using R ggplot2 with example.

## R ggplot Histogram Syntax

The syntax to draw a ggplot Histogram in R Programming is

`geom_histogram(data = NULL, binwidth = NULL, bins = NULL)`

and the complex syntax behind this Histogram is:

```geom_histogram(mapping = NULL, data = NULL, stat = "bin",
binwidth = NULL, bins = NULL, position = "stack", ...,
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)```

Before we get into the R ggplot Histogram example, let us the see the data that we are going to use for this Histogram example. For this demonstration, we are going to use the diamonds data set that is provided by the R, and the data inside this data set is:

## Create R ggplot2 Histogram

In this example, we show how to create a Histogram using the ggplot2 package. For this demo, we are going to use the above-shown diamonds data set provided by the R Studio.

TIP: ggplot2 package in R not installed by default. Please refer Install R Packages article to install the package in R Programming.

```# Create R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Default way to Create a Histogram
ggplot(data = diamonds, aes(x = price)) + geom_histogram()```

### Change bins of a R ggplot2 Histogram

In this example, we show how to change the number of bins (range, or breaks) in an R ggplot histogram. By default, geom_histogram() provides 30 bins but, you can alter the value as per the requirements.

NOTE: If you require to import data from external files, then please refer to the R Read CSV article to import the CSV file.

```# Change bins of an R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Default way to Create a Histogram
ggplot(data = diamonds, aes(x = price)) + geom_histogram(bins = 10)```

### Change binwidth of an R ggplot2 Histogram

This example shows how to change the number of binwidth (overriding the bins) in an R ggplot histogram.

```# Change binwidth of an R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price)) + geom_histogram(binwidth = 10)```

TIP: Use bandwidth = 2000 to get the same histogram that we created with bins = 10.

### Change Colors of an R ggplot2 Histogram

In this example, we change the color of a histogram drawn by the ggplot2.

• color: Please specify the color to use for your bar borders in a histogram. For example “red”, “blue”, “green” etc. In this example, we are assigning the “red” color to borders.
• fill: You have to specify the Bar Colors. In this example, we are assigning the sea green color.
```# Change Color of an R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price)) + geom_histogram(binwidth = 250,
fill = "seagreen",
color = "red")```

#### Change Color of an R ggplot2 Histogram example 2

Let us see how to change the color of a ggplot2 histogram in r based on the column data. In this example, we are assigning the cut column as the fill attribute. You can try changing it to any other column.

```# Change Color of an R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price, fill = cut)) +
geom_histogram(binwidth = 250, color = "red")```

#### Change Color of an R ggplot2 Histogram example 3

In this example, we are changing the fill attribute to clarity and removed the borders of each bar in an r ggplot histogram.

```# Change Color of an R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price, fill = clarity)) +
geom_histogram(binwidth = 250)```

### Change Line Type of an R ggplot2 Histogram

In this example, we change the borderlines of each bar in an r ggplot histogram.

```# Change Line Type of a R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price, fill = clarity)) +
geom_histogram(binwidth = 250, color = "midnightblue", linetype = "longdash")```

TIP: In R programming, 0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash. So, use numbers or string

### Alter Legend position of an R ggplot2 Histogram

By default, r ggplot position the legend at the right side of a Histogram. In this example, we change the legend position from right to top. Remember, You can use a legend.position = “none” to completely remove the legend.

```# Change Legend Position of an R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price, fill = cut)) +
geom_histogram(binwidth = 250, color = "midnightblue") +
theme(legend.position = "top")```

## How to use Facets in R ggplot2 Histogram

This example draws multiple histograms in R ggplot by dividing the data based on column values.

```# Use Facet in R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price, fill = cut)) +
geom_histogram(binwidth = 250, color = "gold") +
facet_wrap(~ cut) # divide the histogram, based on Cut```

### How to use Facets in R ggplot2 Histogram example 2

By default, facet_wrap() assign the same y-axis to all the histogram. But, you can change it (giving independent axis) to each histogram by adding one more attribute called scale.

```# Use Facet in R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price, fill = cut)) +
geom_histogram(binwidth = 250, color = "gold") +
facet_wrap(~ cut, scale = "free")```

### Assigning names to R ggplot Histogram

Let us assign names to ggplot2 Histogram, X-Axis, and Y-Axis using labs function in r

```# Add labels to R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price, fill = cut)) +
geom_histogram(binwidth = 250, color = "midnightblue") +
labs(title="GGPLOT Histogram", x="Price in Dollars", y="Count")
# Or you can add labs one more time to add X, Y axis names
+ labs(x="Price in Dollars", y="Count")```

### Change Axis limits of a ggplot Histogram in R

Let us change the default axis values in a ggplot histogram in r

• xlim: This argument can help you to specify the limits for the X-Axis
• ylim: It helps to specify the Y-Axis limits. In this example, we are changing the default x-axis limit to (0, 20000), and y-axis limit to (0, 8000)
```# Changing Axis in a R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price, fill = cut)) +
geom_histogram(binwidth = 250, color = "midnightblue") +
labs(title="GGPLOT Histogram", x="Price in Dollars", y="Count") +
xlim(0, 20000) + ylim(0, 8000)```

## Create a R ggplot Histogram with Density

Frequency counts and gives us the number of data points per bin. In real-time, we may be interested in density than the frequency-based histograms because density can give the probability densities. Let us see how to create a ggplot Histogram in r against the Density using geom_density().

```# Create a R ggplot Histogram with Density

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price)) +
geom_histogram(binwidth = 250, aes(y=..density..),
fill = "seagreen", color = "midnightblue") +
geom_density(color = "red") +
labs(title="GGPLOT Histogram", x="Price in Dollars", y="Count")```

#### How to use Facets in R ggplot2 Histogram with Density

In this example, we draw the density line to multiple histograms drawn by the ggplot2 in r.

```# Create a R ggplot Histogram with Density

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price, fill = cut)) +
geom_histogram(binwidth = 500, aes(y=..density..), color = "midnightblue") +
facet_wrap(~ cut, scale = "free") +
geom_density(color = "red", lwd = 0.9, alpha = 0.4) +
labs(title="GGPLOT Histogram", x="Price in Dollars", y="Count")```

## Changing Theme of a ggplot2 Histogram

Let us see how to change the default theme of an R ggplot2 histogram

• theme_dark(): We are using this function to change the histogram default theme to dark. Type theme_, then R Studio intelligence shows the list of available options. For example, theme_grey()
```# Change theme of a R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)

# Create a Histogram
ggplot(data = diamonds, aes(x = price, fill = cut)) +
geom_histogram(binwidth = 500, aes(y=..density..), color = "midnightblue") +
facet_wrap(~ cut, scale = "free") +
geom_density(color = "red", lwd = 0.9, alpha = 0.4) +
labs(title="GGPLOT Histogram", x="Price in Dollars", y="Count") +
theme_dark()```