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

5. Scatter Plots > Making a Basic Scatter Plot

Making a Basic Scatter Plot

Problem

You want to make a scatter plot.

Solution

Use geom_point(), and map one variable to x and one to y.

In the heightweight data set, there are a number of columns, but we’ll only use two in this example (Figure 5-1):

library(gcookbook) # For the data set

# List the two columns we'll use
heightweight[, c("ageYear", "heightIn")]

 ageYear heightIn
   11.92     56.3
   12.92     62.3
   12.75     63.3
...
   13.92     62.0
   12.58     59.3
ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()
A basic scatter plot
Figure 5-1. A basic scatter plot

Discussion

To use different shapes in a scatter plot, set shape. A common alternative to the default solid circles (shape #16) is hollow ones (#21), as seen in Figure 5-2 (left):


  

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