The Lattice Histogram in R is useful to visualize the statistical information. Though it looks like Barplot, Histograms display data in equal intervals. In this article we will show you, How to Create a Lattice Histogram using the lattice library, Format its color, adding labels, and drawing multiple Histograms.

## Lattice Histogram in R syntax

The basic syntax to draw the lattice Histogram in R Programming is as shown below

histogram(x, data, allow.multiple, outer = TRUE, auto.key = FALSE, aspect = "fill", panel = lattice.getOption("panel.histogram"), prepanel, scales, strip, groups, xlab, xlim, ylab, ylim, type = c("percent", "count", "density"), nint = if (is.factor(x)) nlevels(x) else round(log2(length(x)) + 1), endpoints = extend.limits(range(as.numeric(x), finite = TRUE), prop = 0.04), breaks, equal.widths = TRUE, drop.unused.levels = lattice.getOption("drop.unused.levels"), ..., lattice.options = NULL, default.scales = list(), default.prepanel = lattice.getOption("prepanel.default.histogram"), subscripts, subset).)

Before we get into the example, let us the see the data that we are going to use for this Lattice Histogram example. * iris* is the date set provided by the R

## Create a Lattice Histogram in R

In this example, we will show you, How to create a Lattice Histogram using the * iris* dataset, which is provided by the R Studio.

**NOTE:** If your requirement is to import data from external files, please refer R Read CSV article

# Create R Histogram using Lattice package # Importing the lattice library library(lattice) # Creating Plot histogram(~ Sepal.Length, data = iris)

**OUTPUT**

### Assigning names to Lattice Histogram in R

In this example, we will show you, How to assign names to Lattice Histogram, X-Axis, and Y-Axis using **main****, xlab**, and** ylab**

**main:**You can change, or provide the Title for your Histogram.**xlab:**Please specify the label for the X-Axis**ylab:**Please specify the label for the Y-Axis

# Assign Names to R Lattice package Histogram # Importing the lattice library library(lattice) # Creating Plot histogram(~ Sepal.Length, data = iris, main = "R Lattice Histogram", xlab = "Length", ylab = "Total Percentage")

**OUTPUT**

### Change Colors of a Lattice Histogram in R

In this example, we will show you, How to change the lattice Histogram colour using * col* argument. Type

*in your console to get the list of colors available in R programming*

**colors()**# Change Colors of a R Lattice package Histogram # Importing the lattice library library(lattice) # Creating Plot histogram(~ Sepal.Length, data = iris, main = "R Lattice Histogram", xlab = "Length", ylab = "Total Percentage", col = c("chocolate2", "seagreen"))

From the above, you can observe that we used two colours for the col argument. It means those two colors will be repeated until the end of bars.

### Changing Bins of a Histogram in R

In this example, we will show you, How to change the Bin size using * breaks* argument. You can use a Vector of values to specify the breakpoints between histogram cells. Use numbers to specify the number of cells a histogram has to return. For example, breaks = 10 means, 10 bars will be returned.

# R Lattice Histogram Example - Changing Bins # Importing the lattice library library(lattice) # Creating Plot histogram(~ Sepal.Length, data = iris, main = "R Lattice Histogram", xlab = "Length", ylab = "Total Percentage", col = c("chocolate2", "seagreen"), breaks = 20)

**OUTPUT**

## Create an R Lattice Histogram with Density

In this example we will show you, How to create a lattice Histogram against the Density and to achieve the same, we have set the * type* argument to density.

# Create R Lattice Histogram - Density # Importing the lattice library library(lattice) # Creating Plot histogram(~ Sepal.Length, data = iris, main = "R Lattice Histogram", xlab = "Length", col = c("chocolate2", "seagreen"), type = "density")

**OUTPUT**

## Multiple Lattice Histograms in R

In this example, we will show you, How to add multiple Histogram to the plot region.

# Create Multiple R Lattice Histogram # Importing the lattice library library(lattice) # Creating Plot histogram(~ Sepal.Length | Species, data = iris, main = "R Lattice Histogram", xlab = "Length", ylab = "Total Percentage", col = c("chocolate2", "seagreen"))

**OUTPUT**

Thank You for Visiting Our Blog