Day 12- Multivariate data visualizations using Lattice plotting system

SaiGayatri Vadali
4 min readApr 30, 2018

--

This post is the twelfth one in the series “Getting started with data science in 30 days using R programming”. Here are the other articles. Before you read this article, it is better to read about Base plotting system as it is closed related to this here.

Lattice plotting system:

Lattice plotting system enables us to view huge information and relationship between different attributes in single window. Also, annotations and borders are inherently made available with a single function call.

Lattice plotting system is provided as “lattice” package in R. It can be installed by using install.packages() function.

>install.packages("lattice")>library(lattice)

xyplot function:

‘xyplot’ is an important function used in lattice plotting system. It creates a scatter plot of the data. ‘Xyplot’ has two main arguments- formula and data. In the below example, the first argument is the formula which is ‘Life.Exp~ Income | region’ . It is like x~y for every ‘z’ where x is ‘Life.Exp’ , y is ‘Income’ and z is ‘region’. Hence the name ‘xyplot’.

> state <- data.frame(state.x77, region = state.region)
> xyplot(Life.Exp ~ Income | region, data = state, layout = c(4, 1))

The display produced in the above image is by default “panel” function associated with “xyplot”. Hence it is panel.xyplot() . This panel can be customized according to user needs too.

Trend lines in xyplot:

Trend lines can be added in ‘xyplot’ using ‘type()’ function. Let us consider, mtcars data and apply the trend lines to it.

> xyplot(mpg ~ hp | factor(cyl), data=mtcars,
+ type=c("p", "r"))

type =’r’ stands for fitting the data with “regression” line. type=’p’ specifies that points also must be shown the cell. Hence, we have joined them both using c().

The above code snippet produces following image with trend lines in each cell of the lattice.

Cloud plot:

Cloud plot is 3D scatter plot used in plotting system. Let’s create a cloud plot on ‘iris’ data set.

>cloud_data <- iris
>head(cloud_data)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa

Let us use the formula for our cloud plot just like ion xyplot as follows

Scatter 3D plot: z ~ x * y

cloud(Sepal.Length ~ Sepal.Length * Petal.Width, 
data = iris)

Colors can be added to the data points as follows

cloud(Sepal.Length ~ Sepal.Length * Petal.Width, 
group = Species, data = iris,
auto.key = TRUE)

BoxPlot in Lattice system:

Box-plot in Base plotting system can be used in Lattice plotting also as follows. In fact, in lattice plotting system, it is more useful as different boxplots among different attributes can be viewed in same image. Let’s use ‘ToothGrowth’ data set for this purpose.

>ToothGrowth$dose <- as.factor(ToothGrowth$dose)
>bwplot(len ~ supp | dose, data = ToothGrowth,
layout = c(3, 1),
xlab = "Dose", ylab = "Length")

Density plot and Histogram:

We have seen density plot and histogram in base plotting system. Same are available with lattice plotting system also. Following is a way of creating a density plot for multiple groups.

>densityplot(~ len, groups = dose, data = ToothGrowth,
plot.points = FALSE, auto.key = TRUE)

Thus lattice plotting system helps us to compare data between different attributes and also provides an easier understanding by providing models in side by side windows in same image.

In the next article, let’s discuss the most widely used plotting system in R which is ‘ggplot2' . Did you find this article helpful? Let me know through commenting and clapping

--

--

SaiGayatri Vadali

An inquisitive Machine Learning Engineer, yoga trainer, fitness freak and a passionate writer!