Lattice Histogram in R

The Lattice Histogram in R is useful to visualize the statistical information. Though it looks like Barplot, Histograms display data in equal intervals. Let us see 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

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 see the data that we are going to use for this Lattice Histogram example. iris is the date set provided by the R

Lattice Histogram in R Example 1

Create a Lattice Histogram in R

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

# Create R Histogram using Lattice package

# Importing the lattice library
library(lattice)

# Creating Plot
histogram(~ Sepal.Length, data = iris)
Lattice Histogram in R Example 2

Assigning names to Lattice Histogram in R

If you require to import R Programming data from external files, please refer R Read CSV article. In this example, we show 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")
Lattice Histogram in R Example 3

Change Colors of a Lattice Histogram in R

In this example, we show you how to change the lattice Histogram color using the col argument. Type colors() in your console to get the list of colors available in R programming.

# 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 code, you can observe that we used two colors for the col argument. It means those two colors repeated until the end of bars.

Lattice Histogram in R Example 4

Changing Bins of a Histogram in R

In this example, we show 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 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)
Lattice Histogram in R Example 5

Create an Lattice Histogram with Density

In this example, we show how to create a lattice Histogram against the Density. 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")
Lattice Histogram in R Example 6

Multiple Lattice Histograms in R

In this example, we 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"))
Lattice Histogram in R Example 7

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.