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

Grid and Lattice Packages in 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.

1. grid package:

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()

2. lattice package:

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)

Summary:

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.

  1. grid package in R:

    • Description: The 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.
    • Code:
      # Install and load the grid package
      install.packages("grid")
      library(grid)
      
      # Create a simple grid plot
      grid.newpage()
      grid.rect()
      
  2. lattice package in R:

    • Description: The 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.
    • Code:
      # Install and load the lattice package
      install.packages("lattice")
      library(lattice)
      
      # Create a simple lattice plot
      xyplot(Sepal.Length ~ Sepal.Width | Species, data = iris)
      
  3. Customizing plots with grid in R:

    • Description: The grid package allows for extensive customization of plots, including modifying colors, lines, and layout.
    • Code:
      # Customizing a plot using the grid package
      grid.newpage()
      grid.rect(gp = gpar(fill = "lightblue"))
      
  4. Creating trellis plots in R:

    • Description: Trellis plots, created using the lattice package, are conditioned plots that show relationships between variables based on different conditions.
    • Code:
      # Creating a trellis plot with lattice
      xyplot(Sepal.Length ~ Sepal.Width | Species, data = iris)
      
  5. Graphics with grid and lattice in R:

    • Description: Combining the grid and lattice packages allows for more advanced and customized lattice plots.
    • Code:
      # Graphics with grid and lattice in R
      grid.newpage()
      print(xyplot(Sepal.Length ~ Sepal.Width | Species, data = iris), newpage = FALSE)
      
  6. R lattice xyplot examples:

    • Description: The xyplot function in the lattice package is used for creating scatterplots, and it supports conditioning plots based on other variables.
    • Code:
      # Examples of using xyplot in lattice
      xyplot(Sepal.Length ~ Sepal.Width | Species, data = iris)
      
  7. Advanced graphics using grid in R:

    • Description: The grid package allows for advanced graphics customization, enabling users to control layout, colors, and other graphical elements.
    • Code:
      # Advanced graphics using the grid package
      grid.newpage()
      grid.rect(gp = gpar(fill = "lightblue"))
      
  8. Lattice graphics vs ggplot2 in R:

    • Description: Lattice graphics and ggplot2 are both popular plotting systems in R. Lattice provides conditioned plots, while ggplot2 follows a grammar of graphics approach.
    • Code:
      # Comparison of lattice graphics and ggplot2
      # (No specific code, but discussing the differences and use cases)