Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

2. Quickly Exploring Data > Creating a Scatter Plot

Creating a Scatter Plot

Problem

You want to create a scatter plot.

Solution

To make a scatter plot (Figure 2-1), use plot() and pass it a vector of x values followed by a vector of y values:

plot(mtcars$wt, mtcars$mpg)
Scatter plot with base graphics
Figure 2-1. Scatter plot with base graphics

With the ggplot2 package, you can get a similar result using qplot() (Figure 2-2):

library(ggplot2)
qplot(mtcars$wt, mtcars$mpg)
Scatter plot with qplot() from ggplot2
Figure 2-2. Scatter plot with qplot() from ggplot2

If the two vectors are already in the same data frame, you can use the following syntax:

qplot(wt, mpg, data=mtcars)
# This is equivalent to:
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()

See Also

See Chapter 5 for more in-depth information about creating scatter plots.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint