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 > Adding Marginal Rugs to a Scatter Plot

Adding Marginal Rugs to a Scatter Plot

Problem

You want to add marginal rugs to a scatter plot.

Solution

Use geom_rug(). For this example (Figure 5-28), we’ll use the faithful data set, which contains data about the Old Faithful geyser in two columns—eruptions, which is the length of each eruption, and waiting, which is the length of time to the next eruption:

ggplot(faithful, aes(x=eruptions, y=waiting)) + geom_point() + geom_rug()
Marginal rug added to a scatter plot
Figure 5-28. Marginal rug added to a scatter plot

Discussion

A marginal rug plot is essentially a one-dimensional scatter plot that can be used to visualize the distribution of data on each axis.

In this particular data set, the marginal rug is not as informative as it could be. The resolution of the waiting variable is in whole minutes, and because of this, the rug lines have a lot of overplotting. To reduce the overplotting, we can jitter the line positions and make them slightly thinner by specifying size (Figure 5-29). This helps the viewer see the distribution more clearly:


  

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