R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
The grid
and lattice
packages in R provide alternative graphics systems to the traditional base graphics system. While ggplot2
(which is also based on grid
) has garnered significant attention for its sophisticated layering logic, lattice
remains popular for its focus on high-level, multivariate graphics.
The grid
package, created by Paul Murrell, provides a lower-level graphics system that is the basis for lattice
and ggplot2
. While you can create visualizations directly with grid
, it's often used indirectly through higher-level packages.
Here's a very basic example using grid
:
library(grid) # Create a viewport vp <- viewport(width = 0.5, height = 0.5) # Push the viewport pushViewport(vp) # Draw a rectangle and a line within the viewport grid.rect(gp = gpar(fill = "skyblue")) grid.lines(unit(c(0.2, 0.8), "npc"), unit(c(0.8, 0.2), "npc"), gp = gpar(col = "red")) # Pop the viewport once done popViewport()
The lattice
package, created by Deepayan Sarkar, is a high-level graphics system based on grid
. It's designed for creating trellis graphics: multivariate plots that show the relationship between multiple variables.
Here are some examples using lattice
:
2.1 Basic scatter plot:
library(lattice) data <- data.frame(x = rnorm(100), y = rnorm(100)) xyplot(y ~ x, data = data)
2.2 Conditional scatter plot:
Plotting data conditioned on one or more other variables is where lattice
shines.
data$group <- factor(sample(1:3, size = 100, replace = TRUE)) xyplot(y ~ x | group, data = data, layout = c(3, 1))
2.3 Histogram:
histogram(~ x, data = data, breaks = 20, col = "skyblue", type = "count")
2.4 Bivariate relationships with panels:
Using the bwplot()
function to visualize box plots:
bwplot(x ~ group, data = data, horizontal = FALSE)
While ggplot2
has become a dominant force in R graphics, there are occasions where lattice
might be preferred, especially when familiarity and specific trellis-style multivariate visualizations are required. Additionally, understanding grid
can be valuable, especially if you want to delve deeper into custom graphics or understand the foundations of the ggplot2
system.
To master these packages, consider going through their comprehensive documentation and vignettes. Deepayan Sarkar's book "Lattice: Multivariate Data Visualization with R" is a definitive guide to the lattice
package.
grid package in R:
grid
package in R provides a low-level graphics system for creating and customizing plots. It is often used as the underlying framework for other high-level plotting packages.# Install and load the grid package install.packages("grid") library(grid) # Create a simple grid plot grid.newpage() grid.rect()
lattice package in R:
lattice
package in R is a high-level graphics system for creating conditioned plots, such as trellis plots. It is built on top of the grid
package.# Install and load the lattice package install.packages("lattice") library(lattice) # Create a simple lattice plot xyplot(Sepal.Length ~ Sepal.Width | Species, data = iris)
Customizing plots with grid in R:
grid
package allows for extensive customization of plots, including modifying colors, lines, and layout.# Customizing a plot using the grid package grid.newpage() grid.rect(gp = gpar(fill = "lightblue"))
Creating trellis plots in R:
lattice
package, are conditioned plots that show relationships between variables based on different conditions.# Creating a trellis plot with lattice xyplot(Sepal.Length ~ Sepal.Width | Species, data = iris)
Graphics with grid and lattice in R:
grid
and lattice
packages allows for more advanced and customized lattice plots.# Graphics with grid and lattice in R grid.newpage() print(xyplot(Sepal.Length ~ Sepal.Width | Species, data = iris), newpage = FALSE)
R lattice xyplot examples:
xyplot
function in the lattice
package is used for creating scatterplots, and it supports conditioning plots based on other variables.# Examples of using xyplot in lattice xyplot(Sepal.Length ~ Sepal.Width | Species, data = iris)
Advanced graphics using grid in R:
grid
package allows for advanced graphics customization, enabling users to control layout, colors, and other graphical elements.# Advanced graphics using the grid package grid.newpage() grid.rect(gp = gpar(fill = "lightblue"))
Lattice graphics vs ggplot2 in R:
# Comparison of lattice graphics and ggplot2 # (No specific code, but discussing the differences and use cases)