The lattice package is a special visualization package, as it takes base R graphics one step further by providing improved default graphs and the ability to display multivariate relationships.

**PACKAGE INSTALLATION & DATA FRAME**

The first thing you have to do is install and load all the packages that we are going to need for our examples. Moreover, we need a dataset to work with. The dataset we chose in our case is “mtcars”, which was extracted from the 1974 Motor Trend US magazine. It comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).

install.packages("lattice")

library(lattice)

attach(mtcars)

You can use `head(mtcars)`

in order to see the variables of your dataset.

**GENERAL FORM**

The general form is: `graph_type(formula, data=)`

The graph_type is selected from the listed below. The formula sets the variable(s) to be displayed and every combination between them. For example, `~var1|A`

means display numeric variable `var1`

for each level of factor `A`

. `v1~v2 | A*B`

means display the relationship between numeric variables `v1`

and `v2`

separately for every combination of factor `A`

and `B`

levels. `~v1`

means display numeric variable x alone.

**GRAPH** **DESCRIPTION** **EXAMPLE**

Barchart – bar chart – `x~A or A~x`

Bwplot – boxplot – `x~A or A~x`

Cloud – 3D scatterplot – `z~x*y|A`

Contourplot- 3D contour plot – `z~x*y`

Densityplot- kernal density plot – `~x|A*B`

Dotplot – dotplot – `~x|A`

Histogram – histogram – `~x`

Levelplot – 3D level plot – `z~y*x`

Parallel – parallel coordinates plot – data frame

Splom – scatterplot matrix – data frame

Stripplot – strip plots – `A~x or x~A`

Xyplot – scatterplot – `y~x|A`

Wireframe – 3D wireframe graph – `z~y*x`

**1.Create Factors With Value Labels**

`gear.f<-factor(gear,levels=c(3,4,5),`

labels=c("3gears","4gears","5gears"))

cyl.f <-factor(cyl,levels=c(4,6,8),

labels=c("4cyl","6cyl","8cyl"))

**2.Kernel Density Plot**

`densityplot(~hp,`

main="Density Plot",

xlab="HP")

**3.Kernel Density Plots by Factor Level**

`densityplot(~hp|cyl.f,`

main="HP by Number of Cylinders",

xlab="HP")

**4.Kernel Density Plots by Factor Level (alternate layout)**

`densityplot(~hp|cyl.f,`

main="HP by Numer of Cylinders",

xlab="HP",

layout=c(1,3))

**5.Boxplots For Each Combination of Two Factors**

`bwplot(cyl.f~hp|gear.f,`

ylab="Cylinders", xlab="HP",

main="HP by Cylinders and Gears",

layout=(c(1,3)))

**6.Scatterplots For Each Combination of Two Factors **

`xyplot(hp~drat|cyl.f*gear.f,`

main="Scatterplots by Cylinders and Gears",

ylab="DRAT", xlab="HP")

**7.3D Scatterplot by Factor Level**

`cloud(hp~drat*qsec|cyl.f,`

main="3D Scatterplot by Cylinders")

**8.Dotplot For Each Combination of Two Factors **

`dotplot(cyl.f~hp|gear.f,`

main="Dotplot Plot by Number of Gears and Cylinders",

xlab="HP")

**9.Scatterplot Matrix **

`splom(mtcars[c(1,3,4,5,6)],`

main="MTCARS Data")

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

## Leave a Reply