© Margot Tollefson 2021
M. TollefsonVisualizing Data in R 4https://doi.org/10.1007/978-1-4842-6831-5_1

1. Introduction: plot( ), qplot( ), and ggplot( ), Plus Some

Margot Tollefson1  
(1)
Stratford, IA, USA
 

R provides many ways to visualize data. Graphics in the R language are generated by functions. Some functions create useful visualizations almost instantly. Other functions combine together to create highly coded sophisticated images. This book shows you how to generate both types of objects.

In the first part of this book, we look in detail at the plot() function – the most basic and versatile of the plotting functions. The functions par(), layout(), and split.screen(), which set global plotting parameters and layout options, are also described, as well as graphics devices.

The second part covers the functions in the ggplot2 package, starting with qplot() and ggplot(). The functions qplot() and ggplot() are simpler to use in many ways than the earlier R functions. Truthfully, the syntax used in the ggplot2 package requires long strings of names and arguments – but the autocomplete function in RStudio makes code entry quite easy.

The third part of the book includes six appendixes containing the canned plotting functions in the graphics and stats packages. The appendixes are sorted by the type of object to be displayed.

1.1 plot( ), par( ), layout( ), and split.screen( )

The function plot() takes an object or objects and creates an image based on the class of the object(s). There are many arguments to plot() that affect the appearance of the image. The arguments can be given values within the call to plot(), or many can be assigned globally using the function par(). After the initial call to plot(), there are several ancillary functions that can be used to add to the image. If having more than one plot on a page is the goal of the user, the functions par(), layout(), or split.screen() can be used to set up the format for multiple plots.

In Chapter 2, we describe the basics of the plot() function. In Chapter 3, the various arguments to plot() are categorized. The ancillary functions for plot() are introduced in Chapter 4. In Chapter 5, the methods (types of plots) for which plot() is defined are presented. In Chapter 6, the possible arguments to the function par() are described, and a way to set up multiple plots using par(), layout() or split.screen() is given. Also, graphics devices are covered.

1.2 qplot( ) and ggplot( )

The functions qplot() and ggplot() in the ggplot2 package provide alternatives to plot(). Default plots generated by qplot() and ggplot() tend to look nicer than default plots generated by plot(). For simple plots, qplot() is sufficient to give an elegant-looking graphic. The function ggplot() provides for more plotting options than qplot(). Some of the syntax used in the ggplot2 package is not used in standard R.

In ggplot2, a theme for a graphic can be defined using theme() or by a canned theme function beginning with “theme_”. The objects to be plotted are “mappings” and are assigned in the function aes(). The type of image to be displayed is a geometry or statistic and is assigned by functions beginning with “geom_” or “stat_”. The appearance of the graphic can be changed by using aes(), “aes_” functions, “geom_” functions, “stat_” functions, and/or other formatting functions. More than one aesthetic, geometry, and statistic can be used to create an image. The appearance of the image can be changed by running a formatting function from within another function or by running a formatting function separately.

In Chapter 7, we look at qplot(), ggplot(), and the syntax of the ggplot2 package. Chapter 8 introduces the theme(), “theme_”, and “element_” functions, as well as the aes() and “aes_” functions. In Chapter 9, geometry, statistic, annotate, and the borders() functions are described. Chapter 10 goes over various functions in the ggplot2 package that also change the appearance of the image.

1.3 The Appendixes

Other than plot(), qplot(), and ggplot(), which are in the base and ggplot2 packages, there are many plotting functions in the graphics and stats packages. The functions are useful for data cleaning, data exploration, and/or model fitting. For many of the functions, the graphical arguments used by plot() can be assigned. The specialized plotting functions are given in the appendixes at the end of this book, along with brief descriptions of what the functions do and how to use them.

Appendix A lists functions used with contingency tables. Appendix B gives functions for continuous variables. Appendix C lists functions that generate multiple plots. Appendix D gives functions that smooth data. Appendix E gives plotting functions used in time series analysis. Appendix F lists the plotting functions that are in the stats and graphics packages and not covered in the first five appendixes.

1.4 Software Versions and Hardware Used in This Book

The versions of R used in this book are R 4.0.1 and R 4.0.3; the versions of RStudio are 1.3.595 and 1.3.1093. Since R and RStudio are constantly changing, the Comprehensive R Archive Network (CRAN) provides news on changes to R. The news for R 4.0.1 and R 4.0.0 can be found at https://cran.r-project.org/doc/manuals/r-release/NEWS.pdf. The beginning of that news pdf is shown in Figure 1-1.
../images/502384_1_En_1_Chapter/502384_1_En_1_Fig1_HTML.jpg
Figure 1-1

R NEWS screenshot

The computer used for the examples is a MacBook Air running macOS Catalina version 10.15.5.

1.5 Graphics Devices

R opens graphics objects in a graphics device. By default, R opens the graphics object on the computer screen, but the object can be written to a file using one of several image formats. Section 6.1 covers graphics devices.

R and RStudio provide ways to save graphics objects to image files by selecting links in the menus of the two programs. R automatically opens and closes the relevant devices when a link is used.

To work with graphics devices through code, see Section 6.1 or the R help pages for “device” and dev.cur(). Both Section 6.1.1 and the help page for “device” have a list of the functions on your device that open a graphics device to create a specific type of image file. Section 6.1.2 and the help page for dev.cur() give a list of the functions that manage graphics devices.

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

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