Data visualization is the most important aspect of any analysis. It is the only thing which conveys all your hard work. As we know, R includes three types of plotting systems. The base plot, ggplot, and lattice plotting. These plotting systems differ from one another based on graphics. You might have experienced much fancier graphics in Python, but R keeps on improving on its graphics part to offer much value in visualizations. In this article, we will talk about lattice in R, an elegant visualization system.
What is a lattice in R?
The lattice in R, which is inspired by trellis graphics, has added more default functions and the ability to display multivariate data to the base plotting. The lattice add-on package is designed to satisfy the better graphic needs and the best part is it needs minimal tuning. It includes many plotting functions such as xyplot, histogram, qqplot, boxplot, cloud plot, and the list goes on.
Let’s see some of the major plotting function associated with lattice in R.
1. xyplot (Scatter plot) in R
You can use xyplot to create a scatter plot. All you need to do, it to install the lattice package and load the required libraries as shown here.
#Loads lattice library library(lattice) #Loads the data df <- datasets::iris #Creats a scatter plot of the data xyplot(df$Sepal.Length~df$Petal.Length, data = df)
With a slight modification in the code, you can create a plot with groups of the “Species” attribute. This will result in three plots that reflect each group distribution.
#Creates a plot with group_by xyplot(df$Sepal.Length~df$Petal.Length | df$Species,data = df, type = c('p','smooth'),scales = 'free')
- Here, the xyplot function will help you to create the scatter plot of the input data.
- The syntax is very simple, you have to mention the attributes of data and the dataframe to get a scatter plot.
2. Histogram in lattice in R
A histogram is the most used plot to understand the data distribution or shape. Again the syntax is very simple, mention the attributes and breaks. Let’s see how it works.
#creates histogram for particular attribute histogram(~df$Sepal.Length,data = df, breaks = 50)
- I have used breaks as 50. Feel free to play with them.
- Also try out other data attributes to plot the histogram.
3. Boxplot in lattice in R
The boxplot is the standard display of data distribution which is based on a 5-number summary. This plot will give you insights into quartiles, min & max, and outliers. Let’s see how we can plot a box plot in the lattice.
#Load the data df <- datasets::BOD #Creates box plot bwplot(df$demand, data = df)
- Here we have used the “BOD” dataset.
- You can go through the summary of the dataset below. This summary is what our boxplot is reflecting.
Time demand Min. :1.000 Min. : 8.30 1st Qu.:2.250 1st Qu.:11.62 Median :3.500 Median :15.80 Mean :3.667 Mean :14.83 3rd Qu.:4.750 3rd Qu.:18.25 Max. :7.000 Max. :19.80
4. Barchart using Lattice
Barcharts includes the rectangle bars which reflect the data distribution. It is mostly used to plot the categorical data. Let’s see how we can use this plot with our data.
#Load the data df <- datasets::airquality #Creates barchart barchart(df$Ozone[1:10]~df$Temp[1:10], data = df,xlab = 'Temparature',ylab = 'Ozone')
- We have created a bar chart and the syntax will be simple just like the above functions.
- You can see the “Ozone” distribution with respect to “Temperature”.
5. Dotplot in R
The dot plots are used to compare the occurrences of the frequency of counts. The visualization includes the dots to represent the frequency.
#Create a vector Demand <- c(3,4,6,5,4,7,8,6,5,9,10,3,2) Supply <- c(1,2,1,3,2,1,4,3,2,1,4,3,4) #Create a dataframe df <- data.frame(Demand,Supply) #Creates a dotplot for input data dotplot(df$Demand~df$Supply, data = df, xlab = 'Supply',ylab = 'Demand',main = 'Dotplot using lattice in R')
- You can observe the frequency of the counts using dot plots in R.
- You can add labels and titles to your plot as shown. Feel free to try out these parameters.
Lattice plotting is an elegant visualization system in R. It offers many functions to create different plots bases on the needs of your multivariate data.
I have illustrated some of the most common plots in this article. Take some time to try out other plotting functions and let me know how you feel. That’s all for now. Happy R!!!
More read: Lattice documentation.