Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating basic scatter plots


This recipe describes how to make scatter plots using some very simple commands. We'll go from a single line of code, which makes a scatter plot from preloaded data, to a script of few lines that produces a scatter plot customized with colors, titles, and axes limits specified by us.

Getting ready

All you need to do to get started is start R. You should have the R prompt on your screen as shown in the following screenshot:

How to do it...

Let's use one of R's built-in datasets called cars to look at the relationship between the speed of cars and the distances taken to stop the cars (recorded in the 1920s).

To make your first scatter plot, type the following command in the R prompt:

plot(cars$dist~cars$speed)

This should bring up a window with the following graph that shows the relationship between the distance travelled by cars plotted with their speeds:

Now, let's tweak the graph to make it look better. Type the following code in the R prompt:

plot(cars$dist~cars$speed...