Chapter 1. R Graphics

R provides a number of well-known facilities that produce a variety of graphs to meaningfully visualize data. It has low-level facilities where we deal with basic shapes to draw graphs and high-level facilities. There are functions available here to produce quality graphs; these functionalities are usually developed using certain combinations of basic shapes. Using R, we can produce traditional plots, the trellis plot, and very high-level graphs inspired by the Grammar of Graphics implemented in the ggplot2 package. The default graphics package is useful for traditional plots, lattice provides facilities to produce trellis graphs, and the ggplot2 package is the most powerful high-level graphical tool in R. Other than these, there are low-level facilities that draw basic shapes, and arranging the shapes in their relative position is an important step in order to create meaningful data visualization. In this chapter, we will introduce both low-level graphics (also known as base graphics) and high-level graphics using different packages. Particularly, the content of this chapter will be as follows:

  • Base graphics using the default package
  • Trellis graphs using lattice
  • Graphs inspired by Grammar of Graphics

Base graphics using the default package

It is well known that R has very powerful data visualization capabilities. The primary reason behind the powerful graphical utility of R is the low-level graphical environment. The grid graphic system of R makes data visualization much more flexible and intuitive. With the help of the grid package, we can draw very basic shapes that can be arranged to produce interesting data visualizations. There are functions in the grid graphics system that draw very basic shapes of a high-level data visualization, including lines, rectangles, circles, and texts along with some other functions that specify where to put which part of the visualization. Through the use of the basic function, we can easily produce components of high-level graphs, such as a rectangle, rounded rectangle, circle, line, and arrow. We will now see how we can produce these basic shapes. In a single visualization, we will show you all the output from the following code snippet:

# Calling grid library
library(grid)

# Creating a rectangle
grid.rect(height=0.25,width=0.25)

# A rounded rectangle
grid.roundrect(height=0.2,width=0.2) 

# A circle
grid.circle(r=0.1)

# Inserting text within the shape
grid.text("R Graphics")
# Drawing a polygon
grid.polygon()
Base graphics using the default package

Basic shapes using the grid package

For any high-level visualization, we can use the basic shapes and arrange them as required. Now, we will list some of the functions for high-level data visualization where the basic shapes have been used:

  • plot: This is a generic function that is used to plot any kind of objects. Most commonly, we use this function for x-y plotting
  • barplot: This function is used to produce a horizontal or vertical bar plot
  • boxplot: This is used to produce a box-whisker plot
  • pie: This is used to produce a pie chart
  • hist: This is used to produce a histogram
  • dotchart: This is used to produce cleveland dot plots
  • image, heatmap, contour, and persp: These functions are used to generate image-like plots
  • qqnorm, qqline, and qqplot: These functions are used to produce plots in order to compare distributions

We will provide specific recipes for each of these functions in the subsequent chapters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.223.12