How to create a scatter plot using lattice package in R?
In this article, we will discuss how to create the scatter plots using lattice package in R programming language.
In R programming, the Lattice package is a data visualization library that consists of various functions to plot different kinds of plots. Using the lattice library we can able to plot various kinds of plots like scatter plot, Box plots, Histograms, 3D scatter plots, Dot plots, Strip plots, Density plots, etc. In order to use the functionalities of the lattice library need to import the library first.
To import the lattice library use the below statement-
Scatter plot using lattice package in R
In R, The Lattice library contains xyplot() method that is used to create a scatter plot. In order to use the xyplot() method, the lattice library needs to be imported first. The syntax of xyplot() method is given below-
Syntax: xyplot( col1~col2, data=dataframeName)
Let’s look into a couple of examples on how to plot the box plot using lattice library.
Example 1: In the below code we created a data frame “stats” and plotted a scatter plot between data in two columns using xyplot() method.
"stats Dataframe" player runs wickets 1 A 200 5 2 B 100 1 3 C 100 6 4 A 150 2 5 B 109 4 6 C 200 2 7 A 270 0 8 B 120 8 9 C 76 1
Example 2: In this example, we plotted a grouped scatter plot for the above-created data frame using xyplot() method.