Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Lattice Histogram in R

by suresh

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

Placed Under: R Programming

  • R Software Download
  • Install R Packages
  • Install R Software
  • Download & Install R Studio
  • R Arithmetic Operators
  • R Comparison Operators
  • R Logical Operators
  • R If Statement
  • R If Else Statement
  • R Else If Statement
  • R Nested If Else
  • R Switch Statement
  • R Break Statement
  • R Next Statement
  • R For Loop
  • R While Loop
  • R Repeat
  • R Vector
  • R Data Frame
  • R List
  • R Arrays
  • R Matrix
  • SQL Server R Services
  • R Read CSV Function
  • R Read table Function
  • R Barplot
  • R Boxplot
  • R Histogram
  • R Pie Chart
  • R Stacked Barplot
  • R Stem and Leaf Plot
  • R Mosaic Plot
  • R ggplot2 Boxplot
  • R ggplot2 Density Plot
  • R ggplot2 Dot Plot
  • R ggplot2 Histogram
  • R ggplot2 Jitter
  • R ggplot2 Line Plot
  • R ggplot2 Scatter Plot
  • R ggplot2 Violin Plot
  • Save R ggplot using ggsave
  • R Lattice Bar chart
  • R Lattice Scatter Plot
  • R Lattice Histogram
  • R Functions
  • R Recursive Functions
  • R abs Function
  • R sqrt Function
  • R ceiling Function
  • R floor Function
  • R round Function

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy