The lattice package in R Programming provides **barchart** to plot Bar Chart. In real-time Lattice Bar chart in R is very useful to compare the data visually. For instance, If we want to compare the sales against region, categories, or product color use this bar chart. In this article we will show you, How to Create Lattice bar Chart, Format bar colors etc with example.

## Lattice Bar chart in R Syntax

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

barchart(x, data,.............)

and the complex syntax behind this bar chart is:

barchart(x, data, panel = lattice.getOption("panel.barchart"), default.prepanel = lattice.getOption("prepanel.default.barchart"), box.ratio = 2, ...)

To demonstrate this, we are going to use the* barley *data set that is provided by the R, and the data inside this data set is:

## Create Lattice Bar chart in R

In this example we will show you, How to create a bar chart in R.

**NOTE :** If your requirement is to import data from external files then please refer R Read CSV article to understand the steps involved in csv file import

# Create Lattice Bar Chart in R Programming # Importing the lattice library library(lattice) # Creating Bar Chart barchart(variety ~ yield, data = barley)

**OUTPUT**

### Assign names to Lattice Bar chart in R

In this example we will show you, How to change the colors of a bar, assign names to bar chart, X-Axis, and Y-Axis using * main, xlab,* and

**ylab**# Chaging Colors, Names of a Lattice Bar Chart in R Programming # Importing the lattice library library(lattice) # Creating Bar Chart barchart(variety ~ yield, data = barley, main = "Bar Chart in R EXample", xlab = "Yield Value", ylab = "Variety", col = c("chocolate", "green", "grey", "blue"))

**OUTPUT**

### Lattice Vertical Bar Chart in R Programming

In this example we will show you, How to change the horizontal bar chart into vertical bar chart using * horiz* argument

# Vertical Lattice Bar Chart in R Programming # Importing the lattice library library(lattice) # Creating Bar Chart barchart(yield ~ variety, data = barley, main = "Bar Chart in R EXample", xlab = "Yield Value", ylab = "Variety", col = c("chocolate", "green", "grey", "blue"), horiz = FALSE)

**OUTPUT**

## Multiple Lattice Bar Charts in R Programming

In this example we will show you, How to divide the bar chart based on the groups. here, scales is used to rotate the x-axis labels to 45 degrees angle.

# Multiple Lattice Bar Chart in R Programming # Importing the lattice library library(lattice) # Creating Bar Chart barchart(yield ~ variety | site, data = barley, groups = year, main = "Bar Chart in R EXample", xlab = "Yield Value", stack = TRUE, auto.key = list(space = "right"), scales = list(x = list(rot = 45)))

**OUTPUT**

### Groups in R Lattice Bar Chart

Here, we are using groups to group the common data. **groups –** variable or expression act as a grouping variable within each panel

# Multiple Lattice Bar Chart in R Programming # Importing the lattice library library(lattice) # Creating Bar Chart barchart(yield ~ variety | site, data = barley, groups = year, main = "Bar Chart in R EXample", xlab = "Yield Value", stack = TRUE, auto.key = list(space = "right"), scales = list(x = list(rot = 45)))

**OUTPUT**

### Layout of Lattice Bar Chart in R

In this example we will show you, How to use layout option in Lattice bar chart.

**Layout:**In general, Lattice Bar Chart consists of several panels arranged in a rectangular array. And, Layout determines this arrangement. This is a numeric vector of length 2 (or 3). It means vector of number of columns, rows, and pages (optional).

# Importing the lattice library library(lattice) # Creating Bar Chart barchart(yield ~ variety | site, data = barley, groups = year, main = "Bar Chart in R EXample", ylab = "Yield Value", stack = TRUE, auto.key = list(space = "right"), scales = list(x = list(rot = 45)), layout = c(1,6))

**OUTPUT**

Thank You for Visiting Our Blog